1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Writer;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Content;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Meta;
|
|
|
9 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\MetaInf;
|
|
|
10 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Mimetype;
|
|
|
11 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Settings;
|
|
|
12 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Styles;
|
|
|
13 |
use PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails;
|
|
|
14 |
use ZipStream\Exception\OverflowException;
|
|
|
15 |
use ZipStream\ZipStream;
|
|
|
16 |
|
|
|
17 |
class Ods extends BaseWriter
|
|
|
18 |
{
|
|
|
19 |
/**
|
|
|
20 |
* Private PhpSpreadsheet.
|
|
|
21 |
*/
|
|
|
22 |
private Spreadsheet $spreadSheet;
|
|
|
23 |
|
|
|
24 |
private Content $writerPartContent;
|
|
|
25 |
|
|
|
26 |
private Meta $writerPartMeta;
|
|
|
27 |
|
|
|
28 |
private MetaInf $writerPartMetaInf;
|
|
|
29 |
|
|
|
30 |
private Mimetype $writerPartMimetype;
|
|
|
31 |
|
|
|
32 |
private Settings $writerPartSettings;
|
|
|
33 |
|
|
|
34 |
private Styles $writerPartStyles;
|
|
|
35 |
|
|
|
36 |
private Thumbnails $writerPartThumbnails;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Create a new Ods.
|
|
|
40 |
*/
|
|
|
41 |
public function __construct(Spreadsheet $spreadsheet)
|
|
|
42 |
{
|
|
|
43 |
$this->setSpreadsheet($spreadsheet);
|
|
|
44 |
|
|
|
45 |
$this->writerPartContent = new Content($this);
|
|
|
46 |
$this->writerPartMeta = new Meta($this);
|
|
|
47 |
$this->writerPartMetaInf = new MetaInf($this);
|
|
|
48 |
$this->writerPartMimetype = new Mimetype($this);
|
|
|
49 |
$this->writerPartSettings = new Settings($this);
|
|
|
50 |
$this->writerPartStyles = new Styles($this);
|
|
|
51 |
$this->writerPartThumbnails = new Thumbnails($this);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public function getWriterPartContent(): Content
|
|
|
55 |
{
|
|
|
56 |
return $this->writerPartContent;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public function getWriterPartMeta(): Meta
|
|
|
60 |
{
|
|
|
61 |
return $this->writerPartMeta;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public function getWriterPartMetaInf(): MetaInf
|
|
|
65 |
{
|
|
|
66 |
return $this->writerPartMetaInf;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public function getWriterPartMimetype(): Mimetype
|
|
|
70 |
{
|
|
|
71 |
return $this->writerPartMimetype;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public function getWriterPartSettings(): Settings
|
|
|
75 |
{
|
|
|
76 |
return $this->writerPartSettings;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public function getWriterPartStyles(): Styles
|
|
|
80 |
{
|
|
|
81 |
return $this->writerPartStyles;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public function getWriterPartThumbnails(): Thumbnails
|
|
|
85 |
{
|
|
|
86 |
return $this->writerPartThumbnails;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Save PhpSpreadsheet to file.
|
|
|
91 |
*
|
|
|
92 |
* @param resource|string $filename
|
|
|
93 |
*/
|
|
|
94 |
public function save($filename, int $flags = 0): void
|
|
|
95 |
{
|
|
|
96 |
$this->processFlags($flags);
|
|
|
97 |
|
|
|
98 |
// garbage collect
|
|
|
99 |
$this->spreadSheet->garbageCollect();
|
|
|
100 |
|
|
|
101 |
$this->openFileHandle($filename);
|
|
|
102 |
|
|
|
103 |
$zip = $this->createZip();
|
|
|
104 |
|
|
|
105 |
$zip->addFile('META-INF/manifest.xml', $this->getWriterPartMetaInf()->write());
|
|
|
106 |
$zip->addFile('Thumbnails/thumbnail.png', $this->getWriterPartthumbnails()->write());
|
|
|
107 |
// Settings always need to be written before Content; Styles after Content
|
|
|
108 |
$zip->addFile('settings.xml', $this->getWriterPartsettings()->write());
|
|
|
109 |
$zip->addFile('content.xml', $this->getWriterPartcontent()->write());
|
|
|
110 |
$zip->addFile('meta.xml', $this->getWriterPartmeta()->write());
|
|
|
111 |
$zip->addFile('mimetype', $this->getWriterPartmimetype()->write());
|
|
|
112 |
$zip->addFile('styles.xml', $this->getWriterPartstyles()->write());
|
|
|
113 |
|
|
|
114 |
// Close file
|
|
|
115 |
try {
|
|
|
116 |
$zip->finish();
|
|
|
117 |
} catch (OverflowException) {
|
|
|
118 |
throw new WriterException('Could not close resource.');
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
$this->maybeCloseFileHandle();
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Create zip object.
|
|
|
126 |
*/
|
|
|
127 |
private function createZip(): ZipStream
|
|
|
128 |
{
|
|
|
129 |
// Try opening the ZIP file
|
|
|
130 |
if (!is_resource($this->fileHandle)) {
|
|
|
131 |
throw new WriterException('Could not open resource for writing.');
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
// Create new ZIP stream
|
|
|
135 |
return ZipStream0::newZipStream($this->fileHandle);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Get Spreadsheet object.
|
|
|
140 |
*/
|
|
|
141 |
public function getSpreadsheet(): Spreadsheet
|
|
|
142 |
{
|
|
|
143 |
return $this->spreadSheet;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Set Spreadsheet object.
|
|
|
148 |
*
|
|
|
149 |
* @param Spreadsheet $spreadsheet PhpSpreadsheet object
|
|
|
150 |
*
|
|
|
151 |
* @return $this
|
|
|
152 |
*/
|
|
|
153 |
public function setSpreadsheet(Spreadsheet $spreadsheet): static
|
|
|
154 |
{
|
|
|
155 |
$this->spreadSheet = $spreadsheet;
|
|
|
156 |
|
|
|
157 |
return $this;
|
|
|
158 |
}
|
|
|
159 |
}
|