Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP插件写入excel文件的方法

PHP插件写入excel文件的方法

小云云
小云云asal
2018-03-21 10:23:461416semak imbas

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

Atas ialah kandungan terperinci PHP插件写入excel文件的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn