1 | <?php |
||
17 | class Worksheet implements WorksheetInterface |
||
18 | { |
||
19 | const SHEET_XML_FILE_HEADER = <<<EOD |
||
20 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
||
21 | <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> |
||
22 | EOD; |
||
23 | |||
24 | /** @var \Box\Spout\Writer\Common\Sheet The "external" sheet */ |
||
25 | protected $externalSheet; |
||
26 | |||
27 | /** @var string Path to the XML file that will contain the sheet data */ |
||
28 | protected $worksheetFilePath; |
||
29 | |||
30 | /** @var \Box\Spout\Writer\XLSX\Helper\SharedStringsHelper Helper to write shared strings */ |
||
31 | protected $sharedStringsHelper; |
||
32 | |||
33 | /** @var bool Whether inline or shared strings should be used */ |
||
34 | protected $shouldUseInlineStrings; |
||
35 | |||
36 | /** @var \Box\Spout\Common\Escaper\XLSX Strings escaper */ |
||
37 | protected $stringsEscaper; |
||
38 | |||
39 | /** @var Resource Pointer to the sheet data file (e.g. xl/worksheets/sheet1.xml) */ |
||
40 | protected $sheetFilePointer; |
||
41 | |||
42 | /** @var int Index of the last written row */ |
||
43 | protected $lastWrittenRowIndex = 0; |
||
44 | |||
45 | /** |
||
46 | * @param \Box\Spout\Writer\Common\Sheet $externalSheet The associated "external" sheet |
||
47 | * @param string $worksheetFilesFolder Temporary folder where the files to create the XLSX will be stored |
||
48 | * @param \Box\Spout\Writer\XLSX\Helper\SharedStringsHelper $sharedStringsHelper Helper for shared strings |
||
49 | * @param bool $shouldUseInlineStrings Whether inline or shared strings should be used |
||
50 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
51 | */ |
||
52 | 117 | public function __construct($externalSheet, $worksheetFilesFolder, $sharedStringsHelper, $shouldUseInlineStrings) |
|
53 | { |
||
54 | 117 | $this->externalSheet = $externalSheet; |
|
55 | 117 | $this->sharedStringsHelper = $sharedStringsHelper; |
|
56 | 117 | $this->shouldUseInlineStrings = $shouldUseInlineStrings; |
|
57 | |||
58 | /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ |
||
59 | 117 | $this->stringsEscaper = \Box\Spout\Common\Escaper\XLSX::getInstance(); |
|
60 | |||
61 | 117 | $this->worksheetFilePath = $worksheetFilesFolder . '/' . strtolower($this->externalSheet->getName()) . '.xml'; |
|
62 | 117 | $this->startSheet(); |
|
63 | 117 | } |
|
64 | |||
65 | /** |
||
66 | * Prepares the worksheet to accept data |
||
67 | * |
||
68 | * @return void |
||
69 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
70 | */ |
||
71 | 117 | protected function startSheet() |
|
72 | { |
||
73 | 117 | $this->sheetFilePointer = fopen($this->worksheetFilePath, 'w'); |
|
74 | 117 | $this->throwIfSheetFilePointerIsNotAvailable(); |
|
75 | |||
76 | 117 | fwrite($this->sheetFilePointer, self::SHEET_XML_FILE_HEADER); |
|
77 | 117 | fwrite($this->sheetFilePointer, '<sheetData>'); |
|
78 | 117 | } |
|
79 | |||
80 | /** |
||
81 | * Checks if the book has been created. Throws an exception if not created yet. |
||
82 | * |
||
83 | * @return void |
||
84 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
85 | */ |
||
86 | 117 | protected function throwIfSheetFilePointerIsNotAvailable() |
|
87 | { |
||
88 | 117 | if (!$this->sheetFilePointer) { |
|
89 | throw new IOException('Unable to open sheet for writing.'); |
||
90 | } |
||
91 | 117 | } |
|
92 | |||
93 | /** |
||
94 | * @return \Box\Spout\Writer\Common\Sheet The "external" sheet |
||
95 | */ |
||
96 | 87 | public function getExternalSheet() |
|
100 | |||
101 | /** |
||
102 | * @return int The index of the last written row |
||
103 | */ |
||
104 | 81 | public function getLastWrittenRowIndex() |
|
108 | |||
109 | /** |
||
110 | * @return int The ID of the worksheet |
||
111 | */ |
||
112 | 84 | public function getId() |
|
117 | |||
118 | /** |
||
119 | * Adds data to the worksheet. |
||
120 | * |
||
121 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
122 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
123 | * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. NULL means use default style. |
||
124 | * @return void |
||
125 | * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written |
||
126 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
127 | */ |
||
128 | 81 | public function addRow($dataRow, $style) |
|
151 | |||
152 | /** |
||
153 | * Build and return xml for a single cell. |
||
154 | * |
||
155 | * @param int $rowIndex |
||
156 | * @param int $cellNumber |
||
157 | * @param mixed $cellValue |
||
158 | * @param int $styleId |
||
159 | * @return string |
||
160 | * @throws InvalidArgumentException |
||
161 | */ |
||
162 | 81 | private function getCellXml($rowIndex, $cellNumber, $cellValue, $styleId) |
|
188 | |||
189 | /** |
||
190 | * Closes the worksheet |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | 84 | public function close() |
|
204 | } |
||
205 |