首页  >  文章  >  后端开发  >  PHP插件写入excel文件的方法

PHP插件写入excel文件的方法

小云云
小云云原创
2018-03-21 10:23:461421浏览

本文主要和大家分享PHP插件写入excel文件的方法,希望能帮助到大家。

调用代码:

<?php

//echo PHP_VERSION;

error_reporting(E_ALL);
ini_set(&#39;display_errors&#39;, TRUE);
ini_set(&#39;display_startup_errors&#39;, TRUE);
date_default_timezone_set(&#39;Europe/London&#39;);

define(&#39;EOL&#39;,(PHP_SAPI == &#39;cli&#39;) ? PHP_EOL : &#39;<br />&#39;);

/** Include PHPExcel */
require_once &#39;Classes/PHPExcel.php&#39;;

// 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(&#39;A1&#39;, &#39;authorname&#39;)
            ->setCellValue(&#39;B1&#39;, &#39;countryid&#39;)
            ->setCellValue(&#39;C1&#39;, &#39;publishtime&#39;)
            ->setCellValue(&#39;D1&#39;, &#39;nodeid&#39;)
            ->setCellValue(&#39;E1&#39;, &#39;nodetitle&#39;)
            ->setCellValue(&#39;F1&#39;, &#39;deviceinfo&#39;);

// 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,&#39;{&#39;);
		$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(&#39;A&#39;.($key+2), $value[&#39;authorname&#39;])
		->setCellValue(&#39;B&#39;.($key+2), $value[&#39;countryid&#39;])
		->setCellValue(&#39;C&#39;.($key+2), $value[&#39;publishtime&#39;])
		->setCellValue(&#39;D&#39;.($key+2), $value[&#39;nodeid&#39;])
		->setCellValue(&#39;E&#39;.($key+2), $value[&#39;nodetitle&#39;])
		->setCellValue(&#39;F&#39;.($key+2), $value[&#39;deviceinfo&#39;]);
}

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle(&#39;Simple&#39;);

// 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, &#39;Excel2007&#39;);
echo __FILE__;
echo str_replace(&#39;.php&#39;, &#39;.xlsx&#39;, __FILE__);
$objWriter->save(str_replace(&#39;.php&#39;, &#39;.xlsx&#39;, __FILE__));

相关推荐:

php将数据写入excel

以上是PHP插件写入excel文件的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn