Home  >  Article  >  Backend Development  >  How to write excel file using PHP plug-in

How to write excel file using PHP plug-in

小云云
小云云Original
2018-03-21 10:23:461373browse

This article mainly shares with you the method of writing excel files with PHP plug-in. I hope it can help you.

Calling code:

<?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__));

Related recommendations:

php writes data to excel

The above is the detailed content of How to write excel file using PHP plug-in. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn