1 | <?php |
||
13 | class Sheet |
||
14 | { |
||
15 | /** |
||
16 | * Contains the <sheetData> section. |
||
17 | * |
||
18 | * @var \SplFileObject |
||
19 | */ |
||
20 | private $sheetData; |
||
21 | |||
22 | /** |
||
23 | * @var RowBuilderInterface |
||
24 | */ |
||
25 | private $rowBuilder; |
||
26 | |||
27 | /** |
||
28 | * Sheet constructor to init SplFileObject and write xml header. |
||
29 | * Optionally supply a cell id like e.g. A2 to add freeze pane. |
||
30 | * It has to be done right away, since everything is immediately |
||
31 | * written to the SplFileObject. |
||
32 | * |
||
33 | * @param RowBuilderInterface $rowBuilder |
||
34 | * @param string|null $freezePaneCellId |
||
35 | */ |
||
36 | 3 | public function __construct(RowBuilderInterface $rowBuilder, $freezePaneCellId = null) |
|
37 | { |
||
38 | 3 | $this->rowBuilder = $rowBuilder; |
|
39 | |||
40 | 3 | $this->sheetData = new \SplFileObject(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'sheet1.xml', 'wb+'); |
|
41 | 3 | $this->sheetData->fwrite('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xml:space="preserve" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">'); |
|
42 | |||
43 | 3 | if (1 == preg_match('~[A-Z]+([1-9]?[0-9]+)$~', $freezePaneCellId, $match)) { |
|
44 | 1 | $this->sheetData->fwrite('<sheetViews><sheetView tabSelected="1" workbookViewId="0" showGridLines="true" showRowColHeaders="1"><pane ySplit="' . (array_pop($match) - 1) . '" topLeftCell="' . $freezePaneCellId . '" activePane="bottomLeft" state="frozen"/></sheetView></sheetViews>'); |
|
45 | 1 | } |
|
46 | |||
47 | 3 | $this->sheetData->fwrite('<sheetData>'); |
|
48 | 3 | } |
|
49 | |||
50 | /** |
||
51 | * Instantiate new sheet with default RowBuilder implementation. |
||
52 | * |
||
53 | * @param string|null $freezePaneCellId |
||
54 | * @return Sheet |
||
55 | */ |
||
56 | 3 | public static function fromDefaults($freezePaneCellId = null) |
|
60 | |||
61 | /** |
||
62 | * Add single data row to sheet and add new style, |
||
63 | * if its not an integer. |
||
64 | * |
||
65 | * @param array $dataRow |
||
66 | * @param int|Style $style |
||
67 | */ |
||
68 | 1 | public function addRow(array $dataRow, $style = 0) |
|
76 | |||
77 | /** |
||
78 | * Add multiple data rows to sheet. |
||
79 | * |
||
80 | * @param array $dataRows |
||
81 | * @param int|Style $style |
||
82 | */ |
||
83 | 1 | public function addRows(array $dataRows, $style = 0) |
|
89 | |||
90 | /** |
||
91 | * Add new style and return its id. |
||
92 | * |
||
93 | * @param Style $style |
||
94 | * @return int |
||
95 | */ |
||
96 | 1 | public function addStyle(Style $style) |
|
100 | |||
101 | /** |
||
102 | * Write closing xml tag and return path to sheet file. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | 2 | public function sheetFilePath() |
|
111 | } |
||
112 |