|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\XLSX; |
|
4
|
|
|
|
|
5
|
|
|
use Box\Spout\Writer\AbstractMultiSheetsWriter; |
|
6
|
|
|
use Box\Spout\Writer\Style\StyleBuilder; |
|
7
|
|
|
use Box\Spout\Writer\XLSX\Internal\Workbook; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Writer |
|
11
|
|
|
* This class provides base support to write data to XLSX files |
|
12
|
|
|
* |
|
13
|
|
|
* @package Box\Spout\Writer\XLSX |
|
14
|
|
|
*/ |
|
15
|
|
|
class Writer extends AbstractMultiSheetsWriter |
|
16
|
|
|
{ |
|
17
|
|
|
/** Default style font values */ |
|
18
|
|
|
const DEFAULT_FONT_SIZE = 12; |
|
19
|
|
|
const DEFAULT_FONT_NAME = 'Calibri'; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string Content-Type value for the header */ |
|
22
|
|
|
protected static $headerContentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string Temporary folder where the files to create the XLSX will be stored */ |
|
25
|
|
|
protected $tempFolder; |
|
26
|
|
|
|
|
27
|
|
|
/** @var bool Whether inline or shared strings should be used - inline string is more memory efficient */ |
|
28
|
|
|
protected $shouldUseInlineStrings = true; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Internal\Workbook The workbook for the XLSX file */ |
|
31
|
|
|
protected $book; |
|
32
|
|
|
|
|
33
|
|
|
/** @var array contain column width information */ |
|
34
|
|
|
protected $columnwidths = array(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Sets a custom temporary folder for creating intermediate files/folders. |
|
38
|
|
|
* This must be set before opening the writer. |
|
39
|
|
|
* |
|
40
|
|
|
* @api |
|
41
|
|
|
* @param string $tempFolder Temporary folder where the files to create the XLSX will be stored |
|
42
|
3 |
|
* @return Writer |
|
43
|
|
|
* @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened |
|
44
|
3 |
|
*/ |
|
45
|
|
|
public function setTempFolder($tempFolder) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
|
48
|
|
|
|
|
49
|
|
|
$this->tempFolder = $tempFolder; |
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Use inline string to be more memory efficient. If set to false, it will use shared strings. |
|
55
|
|
|
* This must be set before opening the writer. |
|
56
|
|
|
* |
|
57
|
|
|
* @api |
|
58
|
|
|
* @param bool $shouldUseInlineStrings Whether inline or shared strings should be used |
|
59
|
54 |
|
* @return Writer |
|
60
|
|
|
* @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened |
|
61
|
54 |
|
*/ |
|
62
|
|
|
public function setShouldUseInlineStrings($shouldUseInlineStrings) |
|
63
|
51 |
|
{ |
|
64
|
51 |
|
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
|
65
|
|
|
|
|
66
|
|
|
$this->shouldUseInlineStrings = $shouldUseInlineStrings; |
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Clear all column width specification |
|
72
|
|
|
* @return Writer |
|
73
|
96 |
|
*/ |
|
74
|
|
|
public function clearColumnWidth() { |
|
75
|
96 |
|
|
|
76
|
96 |
|
$this->columnwidths = array(); |
|
77
|
96 |
|
|
|
78
|
96 |
|
if( $this->book ) |
|
79
|
96 |
|
$this->book->_setColumnWidth( $this->columnwidths ); |
|
80
|
96 |
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
69 |
|
* Add a width definition for the next sheet that will be generated |
|
86
|
|
|
* @param number $width column width |
|
87
|
69 |
|
* @param number $min column position ( A=1 ) where this width should take effect |
|
88
|
|
|
* @param number $max end of range where width take effect ( default to min ) |
|
|
|
|
|
|
89
|
|
|
* @return Writer |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setColumnsWidth($width, $min, $max = null) { |
|
92
|
|
|
|
|
93
|
|
|
if( $max === null ) |
|
94
|
|
|
$max = $min; |
|
95
|
|
|
|
|
96
|
|
|
$this->columnwidths[] = array( |
|
97
|
|
|
'width' => $width, |
|
98
|
|
|
'min' => $min, |
|
99
|
|
|
'max' => $max |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
60 |
|
if( $this->book ) |
|
103
|
|
|
$this->book->_setColumnWidth( $this->columnwidths ); |
|
104
|
60 |
|
|
|
105
|
60 |
|
return $this; |
|
106
|
57 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Configures the write and sets the current sheet pointer to a new sheet. |
|
110
|
|
|
* |
|
111
|
|
|
* @return void |
|
112
|
|
|
* @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing |
|
113
|
111 |
|
*/ |
|
114
|
|
|
protected function openWriter() |
|
115
|
111 |
|
{ |
|
116
|
111 |
|
if (!$this->book) { |
|
117
|
111 |
|
$tempFolder = ($this->tempFolder) ? : sys_get_temp_dir(); |
|
118
|
111 |
|
$this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle, $this->columnwidths ); |
|
119
|
|
|
$this->book->addNewSheetAndMakeItCurrent(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return Internal\Workbook The workbook representing the file to be written |
|
125
|
|
|
*/ |
|
126
|
63 |
|
protected function getWorkbook() |
|
127
|
|
|
{ |
|
128
|
63 |
|
return $this->book; |
|
129
|
63 |
|
} |
|
130
|
63 |
|
|
|
131
|
63 |
|
/** |
|
132
|
|
|
* Adds data to the currently opened writer. |
|
133
|
|
|
* If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination |
|
134
|
|
|
* with the creation of new worksheets if one worksheet has reached its maximum capicity. |
|
135
|
|
|
* |
|
136
|
|
|
* @param array $dataRow Array containing data to be written. |
|
137
|
|
|
* Example $dataRow = ['data1', 1234, null, '', 'data5']; |
|
138
|
|
|
* @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. |
|
139
|
|
|
* @return void |
|
140
|
|
|
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet |
|
141
|
|
|
* @throws \Box\Spout\Common\Exception\IOException If unable to write data |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function addRowToWriter(array $dataRow, $style) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->throwIfBookIsNotAvailable(); |
|
146
|
|
|
$this->book->addRowToCurrentWorksheet($dataRow, $style); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Returns the default style to be applied to rows. |
|
151
|
|
|
* |
|
152
|
|
|
* @return \Box\Spout\Writer\Style\Style |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function getDefaultRowStyle() |
|
155
|
|
|
{ |
|
156
|
|
|
return (new StyleBuilder()) |
|
157
|
|
|
->setFontSize(self::DEFAULT_FONT_SIZE) |
|
158
|
|
|
->setFontName(self::DEFAULT_FONT_NAME) |
|
159
|
|
|
->build(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Closes the writer, preventing any additional writing. |
|
164
|
|
|
* |
|
165
|
|
|
* @return void |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function closeWriter() |
|
168
|
|
|
{ |
|
169
|
|
|
if ($this->book) { |
|
170
|
|
|
$this->book->close($this->filePointer); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.