ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# XLSWriter Excel操作(\*) 一个高效,快速的xlsx文件导出扩展 可用于在Excel 2007及以上版本XLSX文件中写入多个工作表的文本,数字,公式和超链接 <https://pecl.php.net/package/xlswriter> [文档地址](https://xlswriter-docs.viest.me/zh-cn/kuai-su-shang-shou/chuang-jian-wen-jian) ### **为什么使用xlswriter** 请参考下方对比图;由于内存原因,PHPExcel数据量`相对较大`的情况下无法正常工作,虽然可以通过`修改memory_limit`配置来解决内存问题,但完成工作的时间可能会更长; (PHPExcel是一个处理Excel,CVS文件的开源框架,可以使用它来读取、写入不同格式的电子表格,这也是PHP至今最通用的Excel处理工具,但是它有一个非常致命的缺点: 特别占内存;但不幸的是PHPExcel官方已不再维护了这个项目了,官方团队在github上又起了一个新项目,叫PhpSpreadsheet) - [Predefined Constants](https://www.php.net/manual/en/xlswriter.constants.php) - [Vtiful\\Kernel\\Excel](https://www.php.net/manual/en/class.vtiful-kernel-excel.php)— The Vtiful\\Kernel\\Excel 类 - [Vtiful\\Kernel\\Excel::addSheet](https://www.php.net/manual/en/vtiful-kernel-excel.addSheet.php)— addSheet - [Vtiful\\Kernel\\Excel::autoFilter](https://www.php.net/manual/en/vtiful-kernel-excel.autoFilter.php)— autoFilter - [Vtiful\\Kernel\\Excel::constMemory](https://www.php.net/manual/en/vtiful-kernel-excel.constMemory.php)— constMemory - [Vtiful\\Kernel\\Excel::\_\_construct](https://www.php.net/manual/en/vtiful-kernel-excel.construct.php)— constructor - [Vtiful\\Kernel\\Excel::data](https://www.php.net/manual/en/vtiful-kernel-excel.data.php)— data - [Vtiful\\Kernel\\Excel::fileName](https://www.php.net/manual/en/vtiful-kernel-excel.filename.php)— fileName - [Vtiful\\Kernel\\Excel::getHandle](https://www.php.net/manual/en/vtiful-kernel-excel.getHandle.php)— getHandle - [Vtiful\\Kernel\\Excel::header](https://www.php.net/manual/en/vtiful-kernel-excel.header.php)— header - [Vtiful\\Kernel\\Excel::insertFormula](https://www.php.net/manual/en/vtiful-kernel-excel.insertFormula.php)— insertFormula - [Vtiful\\Kernel\\Excel::insertImage](https://www.php.net/manual/en/vtiful-kernel-excel.insertImage.php)— insertImage - [Vtiful\\Kernel\\Excel::insertText](https://www.php.net/manual/en/vtiful-kernel-excel.insertText.php)— insertText - [Vtiful\\Kernel\\Excel::mergeCells](https://www.php.net/manual/en/vtiful-kernel-excel.mergeCells.php)— mergeCells - [Vtiful\\Kernel\\Excel::output](https://www.php.net/manual/en/vtiful-kernel-excel.output.php)— output - [Vtiful\\Kernel\\Excel::setColumn](https://www.php.net/manual/en/vtiful-kernel-excel.setColumn.php)— setColumn - [Vtiful\\Kernel\\Excel::setRow](https://www.php.net/manual/en/vtiful-kernel-excel.setRow.php)— setRow - - - - - - - [Vtiful\\Kernel\\Format](https://www.php.net/manual/en/class.vtiful-kernel-format.php)— The Vtiful\\Kernel\\Format 类 - [Vtiful\\Kernel\\Format::align](https://www.php.net/manual/en/vtiful-kernel-format.align.php)— align - [Vtiful\\Kernel\\Format::bold](https://www.php.net/manual/en/vtiful-kernel-format.bold.php)— bold - [Vtiful\\Kernel\\Format::italic](https://www.php.net/manual/en/vtiful-kernel-format.italic.php)— italic - [Vtiful\\Kernel\\Format::underline](https://www.php.net/manual/en/vtiful-kernel-format.underline.php)— underline 例子: # 导出文件 ``` <pre class="calibre17">``` $config <span class="token1">=</span> <span class="token3">[</span><span class="token2">'path'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'/home/viest'</span><span class="token3">]</span><span class="token3">;</span><span class="token">//如果路径下有相同命名的文件,新文件会覆盖老文件</span> $excel <span class="token1">=</span> <span class="token5">new</span> <span class="token4"><span class="token3">\</span>Vtiful<span class="token3">\</span>Kernel<span class="token3">\</span>Excel</span><span class="token3">(</span>$config<span class="token3">)</span><span class="token3">;</span> <span class="token">// fileName 会自动创建一个工作表,你可以自定义该工作表名称,工作表名称为可选参数</span> $filePath <span class="token1">=</span> $excel<span class="token1">-</span><span class="token1">></span><span class="token4">fileName</span><span class="token3">(</span><span class="token2">'tutorial01.xlsx'</span><span class="token3">,</span> <span class="token2">'sheet1'</span><span class="token3">)</span> <span class="token1">-</span><span class="token1">></span><span class="token4">header</span><span class="token3">(</span><span class="token3">[</span><span class="token2">'Item'</span><span class="token3">,</span> <span class="token2">'Cost'</span><span class="token3">]</span><span class="token3">)</span> <span class="token1">-</span><span class="token1">></span><span class="token4">data</span><span class="token3">(</span><span class="token3">[</span> <span class="token3">[</span><span class="token2">'Rent'</span><span class="token3">,</span> <span class="token6">1000</span><span class="token3">]</span><span class="token3">,</span> <span class="token3">[</span><span class="token2">'Gas'</span><span class="token3">,</span> <span class="token6">100</span><span class="token3">]</span><span class="token3">,</span> <span class="token3">[</span><span class="token2">'Food'</span><span class="token3">,</span> <span class="token6">300</span><span class="token3">]</span><span class="token3">,</span> <span class="token3">[</span><span class="token2">'Gym'</span><span class="token3">,</span> <span class="token6">50</span><span class="token3">]</span><span class="token3">,</span> <span class="token3">]</span><span class="token3">)</span> <span class="token1">-</span><span class="token1">></span><span class="token4">output</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ```