欢迎光临
感谢一路有你

转载 | phpspreadsheet开发手记

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 

PhpSpreadsheet是一个纯PHP类库,使你能够读写Excel、LibreOffic Calc等这样的表格格式。
https://phpspreadsheet.readthedocs.io/en/develop/

列从0开始算,行从1开始算
$sheet->setCellValueByColumnAndRow(0,1,‘特别说明‘);

安装

composer require phpoffice/phpspreadsheet 版本号
默认情况会提示找不到库,上composer找是有的,是因为还没有稳定版,所以要指定版本 1.0.0beta

依赖
The following software is required to develop using PhpSpreadsheet:

  • PHP version 5.6 or newer
  • PHP extension php_zip enabled
  • PHP extension php_xml enabled
  • PHP extension php_gd2 enabled (if not compiled in)

默认使用ZipArchive来压缩保存
注意读写权限

简单示例

 

默认保存到执行php的根目录,以thinkphp为例index.php在D:\wwwroot\thinkphp\public,那么文件就保存在这
注:如果不想保存到文件,可以传入php://outputphp://stdout直接输出(例如html,输出网页)

通过模板来生成文件

全用代码写太累,可以用模板来修改,但是对于动态数据,还是要由代码生成

 

释放内存

为了防止内存泄露,建议用完手动清理

 

单元格

根据索引获取英文列

其中A=0
Cell::stringFromColumnIndex($pColumn)

设置值

 

宽度设置

$this->getColumnDimension($columnIndex)->setWidth($width);
还可以让其自适应(不靠谱,建议自行设置)
$sheet->calculateColumnWidths();

直接输出下载

 

自动计算列宽

 

函数formula

https://phpspreadsheet.readthedocs.io/en/develop/references/function-list-by-name/
https://phpspreadsheet.readthedocs.io/en/develop/topics/calculation-engine/#function-reference

 

赞(0) 打赏
未经允许不得转载:王明昌博客 » 转载 | phpspreadsheet开发手记
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏