Dieser Artikel erläutert Ihnen hauptsächlich die Methode zum Schreiben von Excel-Dateien mit dem PHP-Plug-in. Ich hoffe, er kann Ihnen helfen.
Aufrufcode:
<?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__));
Verwandte Empfehlungen:
Das obige ist der detaillierte Inhalt vonSo schreiben Sie eine Excel-Datei mit dem PHP-Plug-in. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!