本篇文章给大家带来了关于php的相关知识,其中主要跟大家聊一聊xlswriter扩展是什么?怎么使用xlswriter扩展优化excel导出性能,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。
关于xlswriter
xlswriter 是一个 php c 扩展,旨在提升php在导出大数据量时的性能问题,支持 windows / linux 。可用于在 excel 2007+ xlsx 文件中读取数据,插入多个工作表,写入文本、数字、公式、日期、图表、图片和超链接。
它具备以下特性:
一、写入
100%兼容的 excel xlsx 文件完整的 excel 格式合并单元格定义工作表名称过滤器图表数据验证和下拉列表工作表 png/jpeg 图像用于写入大文件的内存优化模式适用于 linux,freebsd,openbsd,os x,windows编译为 32 位和 64 位freebsd 许可证唯一的依赖是 zlib二、读取
完整读取数据光标读取数据按数据类型读取xlsx 转 csv性能对比先感谢网友提供数据下载安装
github源码
https://github.com/viest/php-ext-xlswriter
xlswriter 文档
https://xlswriter-docs.viest.me/zh-cn/an-zhuang/huan-jing-yao-qiu
下载 ide helper
composer require viest/php-ext-xlswriter-ide-helper:dev-master
但是我一直下载失败,于是去github仓库直接下载 https://github.com/viest/php-ext-xlswriter-ide-helper
然后将里面的几个类复制到一个 xlswriter_ide_helper.php 文件里面,将这个文件放到你的项目中就有代码提示了。
安装 xlswriter 扩展
此处在docker中安装
docker exec -it php72-fpm bashcd /usr/local/binpecl install xlswriterdocker-php-ext-enable xlswriterphp -mphp --ri xlswriterversion => 1.3.6docker restart php72-fpm
性能测试:
测试数据:20 列,每列长度为 19 英文字母
xlswriter
phpspreadsheet
php_xlsxwriter
使用示例:
private function rankpersonexport($activityinfo, $list){ $date = date('y-m-d'); $filename = "{$activityinfo['orgname']}-{$activityinfo['name']}-个人排行榜-{$date}"; $header = ['名次', '用户id', '对接账号', '姓名', '电话', '部门id', '一级部门', '二级部门', '三级部门', '总积分', '最后积分时间', "毫秒"]; if (!empty($activityinfo['ext'])) { $extarr = json_decode($activityinfo['ext'], true); foreach ($extarr as $erritem) { array_push($header, $erritem['name']); } } // list $listval = []; foreach($list as $v){ $temp = [ $v['rank'], $v['userid'], $v['username'], $v['nickname'], $v['phone'], $v['departid'], $v['topdepartname'], $v['secdepartname'], $v['thirddepartname'], $v['score'], $v['updatetime'], $v['micro'], ]; if (!empty($v['ext'])) { $extarr = explode('|', $v['ext']); foreach ($extarr as $k2 => $v2) { $erritemarr = explode('^', $v2); array_push($temp, $erritemarr[1]); } } array_push($listval, $temp); } $re = downloadxlsx($filename, $header, $listval); if($re){ return $this->output(0, $re); }else{ return $this->output(1, 'success'); }}
function gettmpdir(): string{ $tmp = ini_get('upload_tmp_dir'); if ($tmp !== false && file_exists($tmp)) { return realpath($tmp); } return realpath(sys_get_temp_dir());}/** * download xlsx file * * @param string $filename * @param array $header * @param array $list * @return string errmsg */function downloadxlsx(string $filename, array $header, array $list): string{ try { $config = ['path' => gettmpdir() . '/']; $excel = (new \vtiful\kernel\excel($config))->filename($filename.'.xlsx', 'sheet1'); $filehandle = $excel->gethandle(); $format1 = new \vtiful\kernel\format($filehandle); $format2 = new \vtiful\kernel\format($filehandle); // title style $titlestyle = $format1->fontsize(16) ->bold() ->font("calibri") ->align(\vtiful\kernel\format::format_align_center, \vtiful\kernel\format::format_align_vertical_center) ->toresource(); // global style $globalstyle = $format2->fontsize(10) ->font("calibri") ->align(\vtiful\kernel\format::format_align_center, \vtiful\kernel\format::format_align_vertical_center) ->border(\vtiful\kernel\format::border_thin) ->toresource(); $headerlen = count($header); // header array_unshift($list, $header); // title $title = array_fill(1, $headerlen - 1, ''); $title[0] = $filename; array_unshift($list, $title); $end = strtoupper(chr(65 + $headerlen - 1)); // column style $excel->setcolumn("a:{$end}", 15, $globalstyle); // title $excel->mergecells("a1:{$end}1", $filename)->setrow("a1", 25, $titlestyle); // 冻结前两行,列不冻结 $excel->freezepanes(2, 0); // 数据 $filepath = $excel->data($list)->output(); header("content-disposition:attachment;filename={$filename}.xlsx"); $re = copy($filepath, 'php://output'); if ($re === false) { $err = 'failed to write output'; } else { $err = ''; } @unlink($filepath); return $err; } catch (\vtiful\kernel\exception $e) { return $e->getmessage(); }}
如果发现下载的文件有时候打不开,那应该是你使用了官方的demo,问题出在 filesize(),这个函数是有缓存的,所以你会发现下载下来的文件和原始的文件大小不一样。要么像我一样不去设置 content-length,要么使用 clearstatcache()手动清除缓存。
实测5w条记录导出耗时1.5s,效果还是很强劲的。
导出效果
推荐学习:《php视频教程》
以上就是详解php用xlswriter优化excel导出性能(附代码示例)的详细内容。