PHP扩展xlswriter使用笔记

xlswriter介绍

xlsxwriter 是一个 PHP C 扩展,可用于在Excel 2007以上版本XLSX文件中写入多个工作表的文本,数字,公式和超链接。

以下为我使用了一小部分功能感觉的优缺点:

优点

  • 内存使用量较小,导出时可使用固定内存模式
  • 写入/读取性能高
  • 支持数字,公式和超链接
  • 支持基本的单元格样式
  • 支持创建多个子表格
  • 支持导出超过10万行的大表格

缺点

  • 仅支持 Office 2007+ xlsx格式的Excel表,不支持2003版本xls格式
  • 仅支持简单的基本功能,功能没有PHPExcel丰富
  • 设置单元格格式等方法较为复杂难用
  • 不支持对已存在的文件追加写入(不支持修改文件),只能创建新文件进行写入,已在的文件会覆盖

环境要求

PHP 版本大于等于 7.0

项目相关地址

xlswriter 下载地址

xlswriter github地址

xlswriter gitee地址

xlswriter 文档地址

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 下载安装包,这里选择最新稳定版本1.3.6
wget https://pecl.php.net/get/xlswriter-1.3.6.tgz
# 解压
tar -zxvf xlswriter-1.3.6.tgz
# 编译
cd xlswriter-1.3.6
/usr/local/php72/bin/phpize
# 如需要读取文件,需要加 --enable-reader 编译参数
./configure --with-php-config=/usr/local/php72/bin/php-config --enable-reader
make && make install
# 配置php.ini
echo "extension=/usr/local/php72/lib/php/extensions/no-debug-non-zts-20170718/xlswriter.so" >> /usr/local/php72/lib/php.ini
# 查看PHP扩展是否配置成功
php72 -m | grep xlswriter

主要使用方法及参数解释

以下是我使用过的一些方法,所有可使用方法请查看ide-helper

ide-helper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* 构造函数
* @param array $config 可传入文件目录地址等参数
*/
public function __construct(array $config);

/**
* 传入Excel文件名及工作表名并返回其文件实例
* @param string $fileName 文件名称
* @param string $sheetName 工作表名称
* @return Excel 返回Excel实例
*/
public function fileName(string $fileName, string $sheetName = 'Sheet1'): self {
return $this;
}

/**
* 在工作表的第一行插入数据
* @param array $header
* @return Excel
*/
public function header(array $header): self {
return $this;
}

/**
* 在工作表上插入数据
* @param array $data 数据数组
* @return Excel
*/
public function data(array $data): self {
return $this;
}

/**
* 生成文件
* @return string
*/
public function output(): string {
return 'FilePath';
}

/**
* 合并单元格
* @param string $range 合并单元格范围,例:A1:C2
* @param string $data 合并单元格的值
* @return Excel
*/
public function MergeCells(string $range, string $data): self {
return $this;
}

/**
* 在Excel文件中添加一个新的工作表
* 工作表名称必须是有效的Excel工作表名称,
* 必须少于32个字符,并且不能包含任何字符:/ \ [ ] : * ?
* 此外,不能对多个工作表使用相同的、不区分大小写的“$sheetName”。
* @param string|NULL $sheetName 工作表名称
* @return Excel
*/
public function addSheet(?string $sheetName): self {
return $this;
}

/**
* 切换工作表
* @param string $sheetName 工作表名称
* @return Excel
*/
public function checkoutSheet(string $sheetName): self {
return $this;
}

/**
* 在单元格上插入数据
* @param int $row 单元格所在行 从0开始(第一行传0)
* @param int $column 单元格所在列 从0开始(第一列为0,A列传0)
* @param int|string|double $data 单元格需要写入的数据
* @param string|null $format 内容格式
* @param resource|null $formatHandle 单元格样式
* @return Excel
*/
public function insertText(int $row, int $column, $data, string $format = NULL, $formatHandle = NULL): self {
return $this;
}

/**
* 辅助函数
* 数字转字符
*/
var_dump(\Vtiful\Kernel\Excel::stringFromColumnIndex(0)); // A
var_dump(\Vtiful\Kernel\Excel::stringFromColumnIndex(28)); // AC

/**
* 辅助函数
* 字符转数字
*/
var_dump(\Vtiful\Kernel\Excel::columnIndexFromString('A')); // 0
var_dump(\Vtiful\Kernel\Excel::columnIndexFromString('AC')); // 28

简单使用

导出

1
2
3
4
5
6
7
8
9
10
11
12
13
// 指定导出文件路径
$config = ['path' => '/home/test'];
$excel = new \Vtiful\Kernel\Excel($config);
// fileName 会自动创建一个工作表,你可以自定义该工作表名称,工作表名称为可选参数
$filePath = $excel->fileName('test.xlsx', 'sheet1')
->header(['Item', 'Cost'])
->data([
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
])
->output();

读取Excel内容

1
2
3
4
5
6
7
8
9
10
11
$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']]