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
| <?php
include_once("xlsxwriter.class.php");
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
//设置 header,用于浏览器下载
$filename = "example.xlsx";
header('Content-disposition: attachment; filename="' . XLSXWriter::sanitize_filename($filename) . '"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
# 表格样式
$styles1 = array(
'font' => 'Arial',
'font-size' => 12,
'font-style' => 'bold', #bold, italic, underline, strikethrough or multiple ie: 'bold,italic'
'color' => '#333',
'fill' => '#fff', # 背景填充
'halign' => 'center', # 水平位置 general, left, right, justify, center
'border' => 'left,right,top,bottom', # 边界,默认是虚线,最好和border-style一起用 left, right, top, bottom, or multiple ie: 'top,left'
'border-style' => 'thin', # 边框样式,要先设置border thin, medium, thick, dashDot, dashDotDot, dashed, dotted, double, hair, mediumDashDot, mediumDashDotDot, mediumDashed, slantDashDot
'border-color' => '#333', # 边框颜色 #RRGGBB, ie: #ff99cc or #f9c
'valign' => 'center', # 垂直位置 bottom, center, distributed
'height' => 50, # 行高
// 'collapsed' => true, # 未知
// 'hidden' => true, # 隐藏行
);
# 每列标题头
$header = array(
'created' => 'date',
'product_id' => 'integer',
'quantity' => '#,##0.00', #价格 #,##0.00表示小数位两个,减少或增加改变长度
'amount' => 'price',
'description' => 'string',
'tax' => '[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00',
);
# 列样式
$col_options = [
'widths' => [20, 30, 20, 40, 40], # 宽度
'auto_filter' => true, # 筛选
// 'freeze_rows' => 2, # 冻结
// 'freeze_columns' => 2, # 冻结
// 'suppress_row' => true,
];
# 表数据
$rows = array(
array('2015-01-01', '1', '-50.5', '2010-01-01 23:00:00', '2012-12-31 23:00:00', '=D2'),
array('2003', '=B1', '23.5', '2010-01-01 00:00:00', '2012-12-31 00:00:00', '=D2*0.05'),
);
$writer = new XLSXWriter();
$writer->setTitle('标题');
$writer->setSubject('主题');
$writer->setAuthor('作者名字');
$writer->setCompany('公司名字');
$writer->setKeywords('关键字');
$writer->setDescription('描述');
$writer->setTempDir('临时目录');
# 合并单元格,第一行的大标题
$writer->markMergedCell('Sheet1', $start_row = 0, $start_col = 0, $end_row = 0, $end_col = 5);
# 每列标题头
$writer->writeSheetHeader('Sheet1', $header, $col_options);
# 表数据行插入
foreach ($rows as $row) {
$writer->writeSheetRow('Sheet1', $row, $styles1);
}
#统计行数 返回行数
$writer->countSheetRows('Sheet1');
# 输出文档
$writer->writeToStdOut();
// $writer->writeToFile('example.xlsx');
// echo $writer->writeToString(); #没什么卵用
// $writer->log('错误信息'); # 控制台输出错误信息 数据支持数组、字符串
exit(0);
|