farmani / php_xlsxwriter
PHP 用于编写 XLSX 文件的库
0.23
2016-07-26 04:54 UTC
Requires
- php: ~5.6.0
Requires (Dev)
- phpunit/phpunit: 4.3.*
This package is not auto-updated.
Last update: 2024-09-14 18:58:48 UTC
README
此库设计为轻量级,内存使用相对较低。
它设计为输出 Excel 工作表,以 (Office 2007+) xlsx 格式,仅支持基本功能
- 支持 PHP 5.2.1+
- 接收 UTF-8 编码的输入
- 多工作表
- 支持货币/日期/数字单元格格式化,简单公式
如果在使用 PHPExcel 编写电子表格时遇到内存不足的情况,请尝试使用此库。
简单的 PHP CLI 示例
$data = array( array('year','month','amount'), array('2003','1','220'), array('2003','2','153.5'), ); $writer = new XLSXWriter(); $writer->writeSheet($data); $writer->writeToFile('output.xlsx');
多工作表
$data1 = array( array('5','3'), array('1','6'), ); $data2 = array( array('2','7','9'), array('4','8','0'), ); $writer = new XLSXWriter(); $writer->setAuthor('Doc Author'); $writer->writeSheet($data1,'Sheet1'); $writer->writeSheet($data2,'Sheet2'); echo $writer->writeToString();
简单/高级单元格格式
//simple formats: date, datetime, integer, dollar, euro, string $header = array( 'created'=>'date', 'product_id'=>'integer', 'quantity'=>'#,##0', 'amount'=>'dollar', 'description'=>'string', 'tax'=>'[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00', ); $data = array( array('2015-01-01',873,1,'44.00','misc','=D2*0.05'), array('2015-01-12',324,2,'88.00','none','=D3*0.05'), ); $writer = new XLSXWriter(); $writer->writeSheet($data,'Sheet1', $header); $writer->writeToFile('example.xlsx');
使用 50000 行进行负载测试: (运行速度快,内存使用低)
include_once("xlsxwriter.class.php"); $header = array('c1'=>'string','c2'=>'string','c3'=>'string','c4'=>'string'); $writer = new XLSXWriter(); $writer->writeSheetHeader('Sheet1', $header );//optional for($i=0; $i<50000; $i++) { $writer->writeSheetRow('Sheet1', array(rand()%10000,rand()%10000,rand()%10000,rand()%10000) ); } $writer->writeToFile('output.xlsx'); echo '#'.floor((memory_get_peak_usage())/1024/1024)."MB"."\n";