xlswriter是一个 PHP C 扩展,可用于在 Excel 2007+ XLSX 文件中读取数据,插入多个工做表,写入文本、数字、公式、日期、图表、图片和超连接。php
它具有如下特性:git
测试环境: Macbook Pro 13 inch, Intel Core i5, 16GB 2133MHz LPDDR3 Memory, 128GB SSD Storage.数据库
两种内存模式导出100万行数据(每行27列,数据类型均为字符串,单个字符串长度为19)
29S
,内存只需 2083MB
;52S
,内存仅需 <1MB
;100万行数据(单行1列,数据类型为INT)
3S
,内存仅 558MB
;2.8S
,内存仅 <1MB
;$excel = new \Vtiful\Kernel\Excel(['path' => '/home/viest']); // fileName 会自动建立一个工做表,你能够自定义该工做表名称,工做表名称为可选参数 $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1') ->header(['Item', 'Cost']) ->data([ ['Rent', 1000], ['Gas', 100], ['Food', 300], ['Gym', 50], ]) ->output();
$excel = new \Vtiful\Kernel\Excel(['path' => './tests']); // 导出测试文件 $filePath = $excel->fileName('tutorial.xlsx') ->header(['Item', 'Cost']) ->output(); // 读取测试文件 $data = $excel->openFile('tutorial.xlsx') ->openSheet() ->getSheetData(); var_dump($data); // [['Item', 'Cost']]
$excel = new \Vtiful\Kernel\Excel(['path' => './tests']); $filePath = $excel->fileName('tutorial.xlsx', 'TestSheet1') ->header(['String', 'Int', 'Double']) ->data([ ['Item_1', 10, 10.9999995], ]) ->output(); // 写入方式打开,将文件指针指向文件末尾。 $fp = fopen('./tests/file.csv', 'a'); // 将 xlsx 文件写入 CSV $resultBoolOne = $excel->openFile('tutorial.xlsx') ->openSheet() ->putCSV($fp); // 将 xlsx 文件追加写入 CSV $resultBoolTwo = $excel->openFile('tutorial.xlsx') ->openSheet() ->putCSV($fp);
String,Int,Double Item_1,10,10.9999995 String,Int,Double Item_1,10,10.9999995
应用场景与常规模式相似,不一样之处在于上层业务能够在回调函数中加工数据,将xlsx中的数据过滤加工并写入csv。异步
https://xlswriter-docs.viest.me函数
Github:https://gitee.com/viest/php-ext-xlswriter工具
Gitee:https://gitee.com/viest/php-ext-xlswriter测试
PECL:https://pecl.php.net/package/xlswriter优化
最后的最后请不要忘记 star
spa