1 | <?php |
||
24 | class WorksheetManager implements WorksheetManagerInterface |
||
25 | { |
||
26 | /** |
||
27 | * Maximum number of characters a cell can contain |
||
28 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007] |
||
29 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3 [Excel 2010] |
||
30 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-ca36e2dc-1f09-4620-b726-67c00b05040f [Excel 2013/2016] |
||
31 | */ |
||
32 | const MAX_CHARACTERS_PER_CELL = 32767; |
||
33 | |||
34 | const SHEET_XML_FILE_HEADER = <<<EOD |
||
35 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
||
36 | <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> |
||
37 | EOD; |
||
38 | |||
39 | /** @var bool Whether inline or shared strings should be used */ |
||
40 | protected $shouldUseInlineStrings; |
||
41 | |||
42 | /** @var SharedStringsHelper Helper to write shared strings */ |
||
43 | private $sharedStringsHelper; |
||
44 | |||
45 | /** @var StyleHelper Helper to work with styles */ |
||
46 | private $styleHelper; |
||
47 | |||
48 | /** @var \Box\Spout\Common\Escaper\XLSX Strings escaper */ |
||
49 | private $stringsEscaper; |
||
50 | |||
51 | /** @var StringHelper String helper */ |
||
52 | private $stringHelper; |
||
53 | |||
54 | /** |
||
55 | * WorksheetManager constructor. |
||
56 | * |
||
57 | * @param OptionsManagerInterface $optionsManager |
||
58 | * @param SharedStringsHelper $sharedStringsHelper |
||
59 | * @param StyleHelper $styleHelper |
||
60 | * @param \Box\Spout\Common\Escaper\XLSX $stringsEscaper |
||
61 | * @param StringHelper $stringHelper |
||
62 | */ |
||
63 | 46 | public function __construct( |
|
76 | |||
77 | /** |
||
78 | * @return SharedStringsHelper |
||
79 | */ |
||
80 | 36 | public function getSharedStringsHelper() |
|
84 | |||
85 | |||
86 | /** |
||
87 | * Prepares the worksheet to accept data |
||
88 | * |
||
89 | * @param Worksheet $worksheet The worksheet to start |
||
90 | * @return void |
||
91 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
92 | */ |
||
93 | 46 | public function startSheet(Worksheet $worksheet) |
|
103 | |||
104 | /** |
||
105 | * Checks if the sheet has been sucessfully created. Throws an exception if not. |
||
106 | * |
||
107 | * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file |
||
108 | * @return void |
||
109 | * @throws IOException If the sheet data file cannot be opened for writing |
||
110 | */ |
||
111 | 46 | private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) |
|
117 | |||
118 | /** |
||
119 | * Adds data to the given worksheet. |
||
120 | * |
||
121 | * @param Worksheet $worksheet The worksheet to add the row to |
||
122 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
123 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
124 | * @param Style $rowStyle Style to be applied to the row. NULL means use default style. |
||
125 | * @return void |
||
126 | * @throws IOException If the data cannot be written |
||
127 | * @throws InvalidArgumentException If a cell value's type is not supported |
||
128 | */ |
||
129 | 33 | public function addRow(Worksheet $worksheet, $dataRow, $rowStyle) |
|
137 | |||
138 | /** |
||
139 | * Returns whether the given row is empty |
||
140 | * |
||
141 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
142 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
143 | * @return bool Whether the given row is empty |
||
144 | */ |
||
145 | 33 | private function isEmptyRow($dataRow) |
|
151 | |||
152 | /** |
||
153 | * Adds non empty row to the worksheet. |
||
154 | * |
||
155 | * @param Worksheet $worksheet The worksheet to add the row to |
||
156 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
157 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
158 | * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. NULL means use default style. |
||
159 | * @return void |
||
160 | * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written |
||
161 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
162 | */ |
||
163 | 33 | private function addNonEmptyRow(Worksheet $worksheet, $dataRow, $style) |
|
183 | |||
184 | /** |
||
185 | * Build and return xml for a single cell. |
||
186 | * |
||
187 | * @param int $rowIndex |
||
188 | * @param int $cellNumber |
||
189 | * @param mixed $cellValue |
||
190 | * @param int $styleId |
||
191 | * @return string |
||
192 | * @throws InvalidArgumentException If the given value cannot be processed |
||
193 | */ |
||
194 | 33 | private function getCellXML($rowIndex, $cellNumber, $cellValue, $styleId) |
|
227 | |||
228 | /** |
||
229 | * Returns the XML fragment for a cell containing a non empty string |
||
230 | * |
||
231 | * @param string $cellValue The cell value |
||
232 | * @return string The XML fragment representing the cell |
||
233 | * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell |
||
234 | */ |
||
235 | 32 | private function getCellXMLFragmentForNonEmptyString($cellValue) |
|
250 | |||
251 | /** |
||
252 | * Closes the worksheet |
||
253 | * |
||
254 | * @param Worksheet $worksheet |
||
255 | * @return void |
||
256 | */ |
||
257 | 36 | public function close(Worksheet $worksheet) |
|
269 | } |