generated from krissss/package-skeleton-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionSpoutTest.php
174 lines (156 loc) · 4.94 KB
/
ExtensionSpoutTest.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
use Kriss\DataExporter\DataExporter;
use Kriss\DataExporter\Writer\Extension\NullSpoutExtend;
use OpenSpout\Common\Entity\Row;
use OpenSpout\Common\Entity\Style\Color;
use OpenSpout\Common\Entity\Style\Style;
use OpenSpout\Writer\WriterInterface;
/**
* 配置 csv
*/
class ConfigCsvExtend extends NullSpoutExtend
{
/**
* @inheritDoc
* @link https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#configuration-for-csv
*/
public function beforeOpen(WriterInterface $writer): void
{
if ($writer instanceof \OpenSpout\Writer\CSV\Writer) {
$options = $writer->getOptions();
$options->FIELD_DELIMITER = '|';
}
}
}
/**
* 默认样式
*/
class DefaultStyleExtend extends NullSpoutExtend
{
/**
* @inheritDoc
*/
public function beforeOpen(WriterInterface $writer): void
{
if ($writer instanceof OpenSpout\Writer\XLSX\Writer || $writer instanceof OpenSpout\Writer\ODS\Writer) {
$style = (new Style())
->setFontName('Arial')
->setFontSize(11)
->setFontColor(Color::GREEN);
$options = $writer->getOptions();
$options->DEFAULT_ROW_STYLE = $style;
}
}
}
/**
* 行和列的样式
*/
class RowCellStyleExtend extends NullSpoutExtend
{
/**
* @inheritDoc
*/
public function buildCellStyle(int|string $colIndex, int $rowIndex): ?Style
{
if ($colIndex === 0 && $rowIndex === 2) {
// 第一列,第二行(A2)
return (new Style())
->setFontColor(Color::RED)
->setFontBold();
}
if ($colIndex === 1 && $rowIndex === 2) {
// 第二列,第二行(B2)
return (new Style())
->setFontColor(Color::WHITE)
->setBackgroundColor(Color::BLACK);
}
if ($colIndex === 0 && $rowIndex === 2) {
// 第一列,第五行(A5)
return (new Style())
->setShouldWrapText();
}
return null;
}
/**
* @inheritDoc
*/
public function buildRowStyle(int $rowIndex): ?Style
{
if ($rowIndex === 3) {
// 第三行
return (new Style())
->setBackgroundColor(Color::BLUE);
}
if ($rowIndex === 1) {
// 第一行
return (new Style())
->setCellAlignment(\OpenSpout\Common\Entity\Style\CellAlignment::CENTER);
}
if ($rowIndex === 6) {
// 第五行
return (new Style())
->setShouldShrinkToFit();
}
return null;
}
/**
* @inheritDoc
*/
public function beforeClose(WriterInterface $writer): void
{
if ($writer instanceof \OpenSpout\Writer\XLSX\Writer) {
$options = $writer->getOptions();
// 合并单元格
// https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#cell-merging
$options->mergeCells(0, 5, 2, 5);
// 设置列宽
// https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#column-widths
$options->setColumnWidth(60, 1);
}
}
}
beforeEach(function () {
$this->source = [
['aa', 'bb', 'cc'],
['aa', 'bb', 'cc'],
['aa', 'bb', 'cc'],
['aa', 'bb', 'cc'],
['This is long For fonts and alignments, OpenSpout does not support all the possible formatting options yet. But you can find the most important ones', '', ''],
['This is long For fonts and alignments, OpenSpout does not support all the possible formatting options yet. But you can find the most important ones', '', ''],
];
$this->filename = __DIR__ . '/../tmp/test';
});
it("Extension Spout: config csv", function () {
$filename = DataExporter::csvSpout($this->source, [
'showHeaders' => false,
'extend' => new ConfigCsvExtend(),
])->saveAs($this->filename);
$options = new \OpenSpout\Reader\CSV\Options();
$options->FIELD_DELIMITER = '|';
$reader = new \OpenSpout\Reader\CSV\Reader($options);
$reader->open($filename);
$firstRow = [];
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
/** @var Row $row */
$firstRow = $row->toArray();
break;
}
}
expect($this->source[0])->toEqual($firstRow);
});
it("Extension Spout: set default style", function () {
DataExporter::xlsxSpout($this->source, [
'extend' => new DefaultStyleExtend(),
])->saveAs($this->filename);
// check by person
expect(true)->toBeTrue();
});
it("Extension Spout: set cell or row style", function () {
DataExporter::xlsxSpout($this->source, [
'showHeaders' => false,
'extend' => new RowCellStyleExtend(),
])->saveAs($this->filename);
// check by person
expect(true)->toBeTrue();
});