PHP导入导出使用php-xlswriter

发表于

官方文档:https://xlswriter-docs.viest.me/zh-cn/an-zhuang/pecl-tui-jian

为什么使用:

1.在自学easyswoole过程中看到文档有 xlsWriter-excel解析项目 这个介绍

2.项目中导出数据

错误信息:

读取缓存
开始执行数据导出 2021-12-01 10:34:04

VirtualAlloc() failed: [0x000005af] ???С??

VirtualAlloc() failed: [0x000005af] ???С??

PHP Fatal Error ‘yii\base\ErrorException’ with message ‘Out of memory (allocated
 1684013056) (tried to allocate 20480 bytes)’

in E:\xxxx\xxxx\vendor\phpoffice\phpspreadsheet\src\PhpSpreadshe
et\Collection\Cells.php:159

Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleFatalError()
#1 {main}
解决办法:

 ini_set(“memory_limit”,”-1″);

结果:

使用步骤:

1. pecl install xlswriter  #安装扩展,添加 extension = xlswriter.so 到 ini 配置

2. composer require viest/php-ext-xlswriter-ide-helper:dev-master

3.执行demo

$config = [
    'path' => '/home/viest' // xlsx文件保存路径
];
$excel  = new \Vtiful\Kernel\Excel($config);
// fileName 会自动创建一个工作表,你可以自定义该工作表名称,工作表名称为可选参数
$filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')
    ->header(['Item', 'Cost'])
    ->data([
        ['Rent', 1000],
        ['Gas',  100],
        ['Food', 300],
        ['Gym',  50],
    ])
    ->output();

至此,success!

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注