Authon: viest [email protected]
# 依赖
sudo apt-get install -y zlib1g-dev
# Excel-Export
git clone https://github.com/viest/php-ext-excel-export.git
cd php-ext-excel-export
git submodule update --init
phpize && ./configure --with-php-config=/path/to/php-config
make && make install
# 添加 extension = xlswriter.so 到 ini 配置
# 依赖
brew install zlib
# Excel-Export
git clone https://github.com/viest/php-ext-excel-export.git
cd php-ext-excel-export
git submodule update --init
phpize && ./configure --with-php-config=/path/to/php-config
make && make install
# 添加 extension = xlswriter.so 到 ini 配置
请预先搭建PHP编译环境,教程详见 php.net
cd 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"
cd PHP_PATH/ext
git clone https://github.com/viest/php-ext-excel-export.git
cd EXT_PATH
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
$config = ['path' => '/home/viest'];
$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();
insertText(int $row, int $column, string|int|double $data[, string $format])
单元格所在行
单元格所在列
需要写入的内容
内容格式
$excel = new \Vtiful\Kernel\Excel($config);
$textFile = $excel->fileName("free.xlsx")
->header(['name', 'money']);
for ($index = 0; $index < 10; $index++) {
$textFile->insertText($index+1, 0, 'viest');
$textFile->insertText($index+1, 1, 10000, '#,##0');
}
$textFile->output();
insertFormula(int $row, int $column, string $formula)
单元格所在行
单元格所在列
公式
$excel = new \Vtiful\Kernel\Excel($config);
$freeFile = $excel->fileName("free.xlsx")
->header(['name', 'money']);
for($index = 1; $index < 10; $index++) {
$textFile->insertText($index, 0, 'viest');
$textFile->insertText($index, 1, 10);
}
$textFile->insertText(12, 0, "Total");
$textFile->insertFormula(12, 1, '=SUM(B2:B11)');
$freeFile->output();
insertImage(int $row, int $column, string $localImagePath[, double $widthScale, double $heightScale])
单元格所在行
单元格所在列
图片路径
对图像X轴进行缩放处理; 默认为1,保持图像原始宽度;值为0.5时,图像宽度为原图的1/2;
对图像轴进行缩放处理; 默认为1,保持图像原始高度;值为0.5时,图像高度为原图的1/2;
$excel = new \Vtiful\Kernel\Excel($config);
$freeFile = $excel->fileName("free.xlsx");
$freeFile->insertImage(5, 0, '/vagrant/ASW-G-66.jpg');
$freeFile->output();
autoFilter(string $scope);
过滤范围
$excel->fileName('test.xlsx')
->header(['name', 'age'])
->data($data)
->autoFilter('A1:B11')
->output();
mergeCells(string $scope, string $data);
单元格范围
数据
$excel->fileName("test.xlsx")
->mergeCells('A1:C1', 'Merge cells')
->output();
setColumn(string $range, double $width [, resource $format]);
单元格范围
单元格宽度
单元格样式
$config = ['path' => './tests'];
$excel = new \Vtiful\Kernel\Excel($config);
$fileObject = $excel->fileName('tutorial01.xlsx');
$fileHandle = $fileObject->getHandle();
$format = new \Vtiful\Kernel\Format($fileHandle);
$boldStyle = $format->bold();
$fileObject->header(['name', 'age'])
->data([['viest', 21]])
->setColumn('A:A', 200, $boldStyle)
->output();
setRow(string $range, double $height [, resource $format]);
单元格范围
单元格高度
单元格样式
$config = ['path' => './tests'];
$excel = new \Vtiful\Kernel\Excel($config);
$fileObject = $excel->fileName('tutorial01.xlsx');
$fileHandle = $fileObject->getHandle();
$format = new \Vtiful\Kernel\Format($fileHandle);
$boldStyle = $format->bold();
$fileObject->header(['name', 'age'])
->data([['viest', 21]])
->setRow('A1', 20, $boldStyle,)
->output();
最大内存使用量 = 最大一行的数据占用量
constMemory(string $fileName);
$config = ['path' => './tests'];
$excel = new \Vtiful\Kernel\Excel($config);
$fileObject = $excel->constMemory('tutorial01.xlsx');
$fileHandle = $fileObject->getHandle();
$format = new \Vtiful\Kernel\Format($fileHandle);
$boldStyle = $format->bold();
$fileObject->header(['name', 'age'])
->data([['viest', 21]])
->setRow($boldStyle, 'A1')
->output();
addSheet([string $sheetName]);
$config = [
'path' => './filePath'
];
$excel = new \Vtiful\Kernel\Excel($config);
// 此处会自动创建一个表格
$fileObject = $excel->fileName("tutorial01.xlsx");
$fileObject->header(['name', 'age'])
->data([['viest', 21]]);
// 向文件中追加一个表格
$fileObject->addSheet()
->header(['name', 'age'])
->data([['wjx', 22]]);
// 最后的最后,输出文件
$filePath = $fileObject->output();