xlsxwriter 是一个 PHP C 扩展,可用于在Excel 2007及以上版本XLSX文件中写入多个工作表的文本,数字,公式和超链接。最近遇到项目的导出功能达百万行,之前使用Thinkphp使用PhpSpreadsheet 导入/导出Excel已经不能满足需求了,所以,现在学习使用xlswriter。
官网:https://xlswriter-docs.viest.me/
扩展库:https://github.com/mk-j/PHP_XLSXWriter
安装
1、下载dll:https://pecl.php.net/package/xlswriter
点击DLL,进入以下页面:
我的电脑是64位,PHPStudy 7.3 NTS环境,所以选择圈中的版本。
2、下载之后解压文件,将.dll文件放置到php扩展目录
3、配置文件修改,找到php.ini文件,添加以下代码:
4、开启配置
5、重启Nginx,phpinfo()查看
安装xlswriter完成
使用
创建一个简单的Excel文件
<?php
$config = ['path' => 'D:phpstudy_proWWWphp-xlswriter.com'];
$excel = new VtifulKernelExcel($config);
$excel->fileName('test.xlsx')
->header(['Item', 'Cost'])
->data([
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
])
->output();
浏览器加载上面php文件,生成test.xlsx文件
导出百万行excel文件
<?php
set_time_limit(0);//取消时间限制
ini_set("memory_limit", -1); //-1不限制内存
$config = ['path' => 'C:phpstudy_proWWWphp-xlswriter.com'];
$excel = new VtifulKernelExcel($config);
$count = 1000000;
$list = [];
for($i=0; $i<$count;$i++){
$list[] = ['cabinet_name','keyNumber','keyName','lender', 'out_checker', 'useReason','depart_name','getDate','putDate','useHour','process', 'isAlarm'];
}
$excel->fileName('test.xlsx')
->header(['柜子名称', '锁号','钥匙名称','借出用户', '借出审批人','借用原因','所属机构','借出时间', '归还时间','使用时长','使用状态','异常类型'])
->data(
$list
)
->output();
查看excel文件
导出完成
原文始发于微信公众号(面试技术):Windows安装与使用PHP扩展xlswriter
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/186820.html