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