欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP插件写入excel文件的方法

程序员文章站 2022-03-07 14:49:24
...
本文主要和大家分享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__));

相关推荐:

php将数据写入excel

以上就是PHP插件写入excel文件的方法的详细内容,更多请关注其它相关文章!

相关标签: excel php 方法