本文主要和大家分享PHP插件写入excel文件的方法,希望能帮助到大家。
调用代码:
<?php //echo PHP_VERSION; error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); date_default_timezone_set('Europe/London'); define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />'); /** Include PHPExcel */ require_once 'Classes/PHPExcel.php'; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // var_dump($objPHPExcel); // Set document properties $objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("PHPExcel Test Document") ->setSubject("PHPExcel Test Document") ->setDescription("Test document for PHPExcel, generated using PHP classes.") ->setKeywords("office PHPExcel php") ->setCategory("Test result file"); // Add some data $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'authorname') ->setCellValue('B1', 'countryid') ->setCellValue('C1', 'publishtime') ->setCellValue('D1', 'nodeid') ->setCellValue('E1', 'nodetitle') ->setCellValue('F1', 'deviceinfo'); // read file and write to msg $handle = @fopen("save_countrymessage_successed.log", "r"); if ($handle) { $h = []; while (($buffer = fgets($handle, 4096)) !== false) { $pos = strpos($buffer,'{'); $message[] = json_decode(substr($buffer,$pos),true); } if (!feof($handle)) { echo "Error: unexpected fgets() fail\n"; } fclose($handle); } //save message to excel. foreach($message AS $key => $value){ // Add some data $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.($key+2), $value['authorname']) ->setCellValue('B'.($key+2), $value['countryid']) ->setCellValue('C'.($key+2), $value['publishtime']) ->setCellValue('D'.($key+2), $value['nodeid']) ->setCellValue('E'.($key+2), $value['nodetitle']) ->setCellValue('F'.($key+2), $value['deviceinfo']); } // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Save Excel 2007 file $callStartTime = microtime(true); echo $callStartTime; //重命名文件 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); echo __FILE__; echo str_replace('.php', '.xlsx', __FILE__); $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
相关推荐:
Atas ialah kandungan terperinci PHP插件写入excel文件的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!