企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
一个高效,快速的xlsx文件导出扩展 可用于在Excel 2007及以上版本XLSX文件中写入多个工作表的文本,数字,公式和超链接 [https://pecl.php.net/package/xlswriter](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 例子: # 导出文件 ``` $config = ['path' => './']; $excel = new \Vtiful\Kernel\Excel($config); $filePath = $excel->fileName('tutorial.xlsx') ->header(['Name', 'age'])//设置标题行(即第一行) ->output(); ``` ![](https://img.kancloud.cn/47/44/47445b1d8b08eb81bcb2505b10fdb74b_182x185.png) ``` $config = ['path' => '/home/viest'];//如果路径下有相同命名的文件,新文件会覆盖老文件 $excel = new \Vtiful\Kernel\Excel($config); // fileName 省略第二个参数会自动创建一个工作表默认Sheet1, // 你可以自定义该工作表名称,工作表名称为可选参数(不填就是默认的Sheet1) $filePath = $excel->fileName('tutorial01.xlsx', '工作表1') ->header(['Item', 'Cost']) ->data([ ['Rent', 1000], ['Gas', 100], ['Food', 300], ['Gym', 50], ]) ->output(); ``` ![](https://img.kancloud.cn/6a/e2/6ae2064cc709de7de6b89f580b75f4b3_268x182.png) ## **追加工作表** ``` $filePath = $excel->fileName('tutorial01.xlsx', '工作表1') ->header(['Item', 'Cost']) ->addSheet("工作表2") ->data([ ['Rent', 1000], ['Gas', 100], ]) ->output(); ``` ![](https://img.kancloud.cn/fd/07/fd07c4dc6a51cee87a4241f2ffd4ab0b_233x113.png) ![](https://img.kancloud.cn/23/c6/23c61309b41e386f89ad0a9b7fe01656_232x115.png) 由上可知在addSheet后调用的header、data等方法是作用与addSheet添加的工作表(即这里的工作表2) ``` $config = ['path' => './']; $excel = new \Vtiful\Kernel\Excel($config); $filePath = $excel->fileName('tutorial01.xlsx', '工作表1') ->header(['Item', 'Cost']) ->data([ ['Rent', 1000], ['Gas', 100], ]) ->addSheet("工作表2") ->header(['Name', 'age']) ->data([ ['Dash', 18], ['Tom', 20], ]) ->output(); ``` ![](https://img.kancloud.cn/c6/7e/c67ef77f9ec59d1c023eec73c29d03fd_229x176.png) ![](https://img.kancloud.cn/0d/d2/0dd2c1322809ea3c6c172218b715c754_285x183.png) ``` $config = ['path' => './']; $excel = new \Vtiful\Kernel\Excel($config); $filePath = $excel->fileName('tutorial01.xlsx', '工作表1') ->header(['Item', 'Cost']) ->data([ ['Rent', 1000], ['Gas', 100], ]) ->addSheet("工作表2") ->header(['Name', 'age']) ->data([ ['Dash', 18], ['Tom', 20], ]) ->checkoutSheet('工作表1')//切换回工作表1并插入数据 ->data([ ['Food', 300], ]) ->output(); ``` ![](https://img.kancloud.cn/9a/f5/9af5bab29a1bceac24728e0c158b0748_258x171.png) # **加载excel大文件** ## **全部读取** **array (new Excel())->sheetList()**:返回所有的工作表 **openSheet(工作表名称)**:打开指定的工作表 ``` $config = ['path' => './']; $excel = new \Vtiful\Kernel\Excel($config); // 打开示例文件 $sheetList = $excel->openFile('test.xlsx') ->sheetList(); dump_export($sheetList);//array ( 0 => 'Sheet1', 1 => 'Sheet2', 2 => 'Sheet3', ) foreach ($sheetList as $sheetName) { if ($sheetName=="Sheet1") { // 通过工作表名称获取工作表数据 $sheetData = $excel ->openSheet($sheetName) ->getSheetData(); dump_export($sheetData); break; } } ``` ## **getSheetData(工作表名称)读取指定的工作表内容** 如未指定工作表名称则默认读取第一个工作表 ``` $data = $excel->openFile('tutorial01.xlsx') ->openSheet() ->getSheetData(); dump($data); //结果: array ( 0 => array ( 0 => 'Item', 1 => 'Cost', ), 1 => array ( 0 => 'Rent', 1 => 1000, ), 2 => array ( 0 => 'Gas', 1 => 100, ), 3 => array ( 0 => 'Food', 1 => 300, ), ) ``` 指定工作表 ~~~ $data = $excel->openFile('tutorial01.xlsx') ->openSheet("工作表2") ->getSheetData(); dump($data); array ( 0 => array ( 0 => 'Name', 1 => 'age', ), 1 => array ( 0 => 'Dash', 1 => 18, ), 2 => array ( 0 => 'Tom', 1 => 20, ), ) ~~~ ## **读取并或略指定以及之前的行** ``` $config = ['path' => './']; $excel = new \Vtiful\Kernel\Excel($config); // 读取数据, 并忽略第二行及以上的行 $data = $excel->openFile('tutorial01.xlsx') ->openSheet("工作表1") ->setSkipRows(2) ->getSheetData(); dump_export($data); 结果: array ( 0 => array ( 0 => 'Gas', 1 => 100, ), 1 => array ( 0 => 'Food', 1 => 300, ), ) ``` ## **游标读取** ``` /* const SKIP_NONE = 0x00; // 不忽略任何单元格、行 const SKIP_EMPTY_ROW = 0x01; // 忽略空行 const SKIP_EMPTY_CELLS = 0x02; // 忽略空单元格(肉眼观察单元格内无数据,并不代表单元格未定义、未使用) const SKIP_EMPTY_VALUE = 0X100; // 忽略单元格空数据 */ $config = ['path' => './']; $excel = new \Vtiful\Kernel\Excel($config); // 读取数据, 并忽略第二行及以上的行 $excel->openFile('tutorial01.xlsx'); // 第二个参数使用 \Vtiful\Kernel\Excel::SKIP_EMPTY_CELLS 忽略空白单元格 // 第二个参数使用 \Vtiful\Kernel\Excel::SKIP_EMPTY_ROW 忽略空白行 $excel->openSheet('工作表1', \Vtiful\Kernel\Excel::SKIP_EMPTY_ROW); // 设置读取时返回的数据的【全局数据类型】 $excel->setType([ \Vtiful\Kernel\Excel::TYPE_STRING, \Vtiful\Kernel\Excel::TYPE_INT, ]); dump($excel->nextRow()); // ['Item', 'Cost'] dump($excel->nextRow()); // ['Rent',100] dump($excel->nextRow()); // ['Gas',100] dump($excel->nextRow()); // ['Food',300] dump($excel->nextRow()); // NULL // 此处判断请使用【!==】运算符进行判断; // 如果使用【!=】进行判断,出现空行时,返回空数组,将导致读取中断; while (($row = $excel->nextRow()) !== NULL) { var_dump($row); } ```