PHP插件 寫入excel文件

調用代碼:

<?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__));
下載地址:

https://download.csdn.net/download/zuoyexingchenzuoye/10298383

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章