PHP-XLSXWriter 是一个用于创建 XLSX (Excel 2007+) 文件的 PHP 库。它提供了一种简单、高效的方式来生成复杂的 Excel 表格,而无需依赖大型框架或库。本文将详细介绍 PHP-XLSXWriter 的使用方法和常见功能。
一、安装
- 环境要求:保证 PHP 版本大于等于 7.0
- 推荐 PECL 安装:
bash
pecl install xlswriter
# 添加 extension = xlswriter.so 到 ini 配置
-
MAC 安装:
-
安装依赖
bashbrew install zlib
-
编译扩展
bashgit clone https://github.com/viest/php-ext-excel-export cd php-ext-excel-export git submodule update --init phpize && ./configure --with-php-config=/path/to/php-config --enable-reader make && make install
-
功能测试
bashmake && make test
-
修改 php.ini
iniextension = xlswriter.so
-
-
UBUNTU 安装:
-
安装依赖
bashapt-get install -y zlib1g-dev
-
其他同 MAC
-
-
ALPINE 安装:
-
添加源
bashvim /etc/apk/repositories # 添加官方 Testing 源 # http://nl.alpinelinux.org/alpine/edge/testing
-
使用 APK 安装
bashapk add php7-pecl-xlswriter
-
-
Windows 安装:
- 下载 DLL
- 编译安装
-
搭建 PHP 编译环境
-
安装依赖
cmdcd PHP_BUILD_PATH/deps DownloadFile http://zlib.net/zlib-1.2.11.tar.gz 7z x zlib-1.2.11.tar.gz > NUL 7z x zlib-1.2.11.tar > NUL cd zlib-1.2.11 cmake -G "Visual Studio 14 2015" -DCMAKE_BUILD_TYPE="Release" -DCMAKE_C_FLAGS_RELEASE="/MT" cmake --build . --config "Release"
-
编译扩展
cmdcd PHP_PATH/ext git clone https://github.com/viest/php-ext-excel-export.git cd php-ext-excel-export git submodule update --init phpize configure.bat --with-xlswriter --with-extra-libs=PATH\zlib-1.2.11\Release --with-extra-includes=PATH\zlib-1.2.11 nmake
-
Docker构建:
-
dockerfile
dockerfileFROM alpine:3.9.6 as build # 构建xlswriter扩展,根据自身需要替换版本号 ENV XLSWRITER_VERSION 1.3.4.1 RUN apk update \ && apk add --no-cache php7-pear php7-dev zlib-dev re2c gcc g++ make curl \ && curl -fsSL "https://pecl.php.net/get/xlswriter-${XLSWRITER_VERSION}.tgz" -o xlswriter.tgz \ && mkdir -p /tmp/xlswriter \ && tar -xf xlswriter.tgz -C /tmp/xlswriter --strip-components=1 \ && rm xlswriter.tgz \ && cd /tmp/xlswriter \ && phpize && ./configure --enable-reader && make && make install #------------------------------------------------------------------------------------------- FROM alpine:3.9.6 # 根据自身需要,添加其它软件 RUN apk update && apk add --no-cache php COPY --from=build /usr/lib/php7/modules/xlswriter.so /usr/lib/php7/modules/xlswriter.so RUN echo "extension=xlswriter.so" > /etc/php7/conf.d/xlswriter.ini
-
构建
bashdocker build -f Dockerfile -t viest/xlswriter:1.3.4.1 .
-
快速上手
1. 导出文件
如果路径下有相同命名的文件,新文件会覆盖老文件
php
$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();
2. 读取文件
- 读取文件已支持 windows 系统,版本号大于等于 1.3.4.1;
- 扩展版本大于等于 1.2.7;
- PECL 安装时将会提示是否开启读取功能,请键入 yes;
编译时需添加 --enable-reader
bash
./configure --enable-reader
php
$config = ['path' => './tests'];
$excel = new \Vtiful\Kernel\Excel($config);
// 导出测试文件
$filePath = $excel->fileName('tutorial.xlsx')
->header(['Item', 'Cost'])
->output();
// 读取测试文件
$data = $excel->openFile('tutorial.xlsx')
->openSheet()
->getSheetData();
var_dump($data); // [['Item', 'Cost']]
3.手动销毁资源
默认所有资源将在变量生命周期结束时全部销毁,除此之外你可以手动关闭销毁。
php
$config = [
'path' => '/home/viest' // xlsx文件保存路径
];
$excel = new \Vtiful\Kernel\Excel($config);
$filePath = $excel->fileName('tutorial01.xlsx')
->header(['Item', 'Cost'])
->output();
// 关闭当前打开的所有文件句柄 并 回收资源
$excel->close();