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 StyleManager Manages styles */ |
||
43 | private $styleManager; |
||
44 | |||
45 | /** @var SharedStringsManager Helper to write shared strings */ |
||
46 | private $sharedStringsManager; |
||
47 | |||
48 | /** @var \Box\Spout\Common\Helper\Escaper\XLSX Strings escaper */ |
||
49 | private $stringsEscaper; |
||
50 | |||
51 | /** @var StringHelper String helper */ |
||
52 | private $stringHelper; |
||
53 | |||
54 | /** @var EntityFactory Factory to create entities */ |
||
55 | private $entityFactory; |
||
56 | |||
57 | /** |
||
58 | * WorksheetManager constructor. |
||
59 | * |
||
60 | * @param OptionsManagerInterface $optionsManager |
||
61 | * @param StyleManager $styleManager |
||
62 | * @param SharedStringsManager $sharedStringsManager |
||
63 | * @param \Box\Spout\Common\Helper\Escaper\XLSX $stringsEscaper |
||
64 | * @param StringHelper $stringHelper |
||
65 | * @param EntityFactory $entityFactory |
||
66 | */ |
||
67 | 46 | public function __construct( |
|
82 | |||
83 | /** |
||
84 | * @return SharedStringsManager |
||
85 | */ |
||
86 | 36 | public function getSharedStringsManager() |
|
90 | |||
91 | |||
92 | /** |
||
93 | * Prepares the worksheet to accept data |
||
94 | * |
||
95 | * @param Worksheet $worksheet The worksheet to start |
||
96 | * @return void |
||
97 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
98 | */ |
||
99 | 46 | public function startSheet(Worksheet $worksheet) |
|
109 | |||
110 | /** |
||
111 | * Checks if the sheet has been sucessfully created. Throws an exception if not. |
||
112 | * |
||
113 | * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file |
||
114 | * @return void |
||
115 | * @throws IOException If the sheet data file cannot be opened for writing |
||
116 | */ |
||
117 | 46 | private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) |
|
123 | |||
124 | /** |
||
125 | * Adds data to the given worksheet. |
||
126 | * |
||
127 | * @param Worksheet $worksheet The worksheet to add the row to |
||
128 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
129 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
130 | * @param Style $rowStyle Style to be applied to the row. NULL means use default style. |
||
131 | * @return void |
||
132 | * @throws IOException If the data cannot be written |
||
133 | * @throws InvalidArgumentException If a cell value's type is not supported |
||
134 | */ |
||
135 | 33 | public function addRow(Worksheet $worksheet, $dataRow, $rowStyle) |
|
143 | |||
144 | /** |
||
145 | * Returns whether the given row is empty |
||
146 | * |
||
147 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
148 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
149 | * @return bool Whether the given row is empty |
||
150 | */ |
||
151 | 33 | private function isEmptyRow($dataRow) |
|
157 | |||
158 | /** |
||
159 | * Adds non empty row to the worksheet. |
||
160 | * |
||
161 | * @param Worksheet $worksheet The worksheet to add the row to |
||
162 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
163 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
164 | * @param \Box\Spout\Writer\Common\Entity\Style\Style $style Style to be applied to the row. NULL means use default style. |
||
165 | * @return void |
||
166 | * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written |
||
167 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
168 | */ |
||
169 | 33 | private function addNonEmptyRow(Worksheet $worksheet, $dataRow, $style) |
|
189 | |||
190 | /** |
||
191 | * Build and return xml for a single cell. |
||
192 | * |
||
193 | * @param int $rowIndex |
||
194 | * @param int $cellNumber |
||
195 | * @param mixed $cellValue |
||
196 | * @param int $styleId |
||
197 | * @return string |
||
198 | * @throws InvalidArgumentException If the given value cannot be processed |
||
199 | */ |
||
200 | 33 | private function getCellXML($rowIndex, $cellNumber, $cellValue, $styleId) |
|
233 | |||
234 | /** |
||
235 | * Returns the XML fragment for a cell containing a non empty string |
||
236 | * |
||
237 | * @param string $cellValue The cell value |
||
238 | * @return string The XML fragment representing the cell |
||
239 | * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell |
||
240 | */ |
||
241 | 32 | private function getCellXMLFragmentForNonEmptyString($cellValue) |
|
256 | |||
257 | /** |
||
258 | * Closes the worksheet |
||
259 | * |
||
260 | * @param Worksheet $worksheet |
||
261 | * @return void |
||
262 | */ |
||
263 | 36 | public function close(Worksheet $worksheet) |
|
275 | } |
||
276 |