1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\XLSX; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Writer\AbstractMultiSheetsWriter; |
6
|
|
|
use Box\Spout\Writer\Common\Options; |
7
|
|
|
use Box\Spout\Writer\Style\StyleBuilder; |
8
|
|
|
use Box\Spout\Writer\XLSX\Internal\Workbook; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Writer |
12
|
|
|
* This class provides base support to write data to XLSX files |
13
|
|
|
* |
14
|
|
|
* @package Box\Spout\Writer\XLSX |
15
|
|
|
*/ |
16
|
|
|
class Writer extends AbstractMultiSheetsWriter |
17
|
|
|
{ |
18
|
|
|
/** @var string Content-Type value for the header */ |
19
|
|
|
protected static $headerContentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; |
20
|
|
|
|
21
|
|
|
/** @var Internal\Workbook The workbook for the XLSX file */ |
22
|
|
|
protected $book; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Sets a custom temporary folder for creating intermediate files/folders. |
26
|
|
|
* This must be set before opening the writer. |
27
|
|
|
* |
28
|
|
|
* @api |
29
|
|
|
* @param string $tempFolder Temporary folder where the files to create the XLSX will be stored |
30
|
|
|
* @return Writer |
31
|
|
|
* @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened |
32
|
|
|
*/ |
33
|
2 |
|
public function setTempFolder($tempFolder) |
34
|
|
|
{ |
35
|
2 |
|
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
36
|
|
|
|
37
|
1 |
|
$this->optionsManager->setOption(Options::TEMP_FOLDER, $tempFolder); |
38
|
1 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Use inline string to be more memory efficient. If set to false, it will use shared strings. |
43
|
|
|
* This must be set before opening the writer. |
44
|
|
|
* |
45
|
|
|
* @api |
46
|
|
|
* @param bool $shouldUseInlineStrings Whether inline or shared strings should be used |
47
|
|
|
* @return Writer |
48
|
|
|
* @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened |
49
|
|
|
*/ |
50
|
30 |
|
public function setShouldUseInlineStrings($shouldUseInlineStrings) |
51
|
|
|
{ |
52
|
30 |
|
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
53
|
|
|
|
54
|
29 |
|
$this->optionsManager->setOption(Options::SHOULD_USE_INLINE_STRINGS, $shouldUseInlineStrings); |
55
|
29 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Configures the write and sets the current sheet pointer to a new sheet. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
* @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing |
63
|
|
|
*/ |
64
|
46 |
|
protected function openWriter() |
65
|
|
|
{ |
66
|
46 |
|
if (!$this->book) { |
67
|
46 |
|
$this->book = new Workbook($this->optionsManager); |
68
|
46 |
|
$this->book->addNewSheetAndMakeItCurrent(); |
69
|
|
|
} |
70
|
46 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return Internal\Workbook The workbook representing the file to be written |
74
|
|
|
*/ |
75
|
36 |
|
protected function getWorkbook() |
76
|
|
|
{ |
77
|
36 |
|
return $this->book; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Adds data to the currently opened writer. |
82
|
|
|
* If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination |
83
|
|
|
* with the creation of new worksheets if one worksheet has reached its maximum capicity. |
84
|
|
|
* |
85
|
|
|
* @param array $dataRow Array containing data to be written. |
86
|
|
|
* Example $dataRow = ['data1', 1234, null, '', 'data5']; |
87
|
|
|
* @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. |
88
|
|
|
* @return void |
89
|
|
|
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet |
90
|
|
|
* @throws \Box\Spout\Common\Exception\IOException If unable to write data |
91
|
|
|
*/ |
92
|
33 |
|
protected function addRowToWriter(array $dataRow, $style) |
93
|
|
|
{ |
94
|
33 |
|
$this->throwIfBookIsNotAvailable(); |
95
|
33 |
|
$this->book->addRowToCurrentWorksheet($dataRow, $style); |
96
|
31 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Closes the writer, preventing any additional writing. |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
36 |
|
protected function closeWriter() |
104
|
|
|
{ |
105
|
36 |
|
if ($this->book) { |
106
|
36 |
|
$this->book->close($this->filePointer); |
107
|
|
|
} |
108
|
36 |
|
} |
109
|
|
|
} |
110
|
|
|
|