首頁  >  文章  >  後端開發  >  PHP外掛寫入excel檔案的方法

PHP外掛寫入excel檔案的方法

小云云
小云云原創
2018-03-21 10:23:461423瀏覽

本文主要跟大家分享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