调用代码:
<?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__));
相关推荐:
php将数据写入excel
以上就是php插件写入excel文件的方法的详细内容。
