|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\XLSX\Internal; |
|
4
|
|
|
|
|
5
|
|
|
use Box\Spout\Writer\Common\Internal\AbstractWorkbook; |
|
6
|
|
|
use Box\Spout\Writer\XLSX\Helper\FileSystemHelper; |
|
7
|
|
|
use Box\Spout\Writer\XLSX\Helper\SharedStringsHelper; |
|
8
|
|
|
use Box\Spout\Writer\XLSX\Helper\StyleHelper; |
|
9
|
|
|
use Box\Spout\Writer\Common\Sheet; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Workbook |
|
13
|
|
|
* Represents a workbook within a XLSX file. |
|
14
|
|
|
* It provides the functions to work with worksheets. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Box\Spout\Writer\XLSX\Internal |
|
17
|
|
|
*/ |
|
18
|
|
|
class Workbook extends AbstractWorkbook |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Maximum number of rows a XLSX sheet can contain |
|
22
|
|
|
* @see http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx |
|
23
|
|
|
*/ |
|
24
|
|
|
protected static $maxRowsPerWorksheet = 1048576; |
|
25
|
|
|
|
|
26
|
|
|
/** @var bool Whether inline or shared strings should be used */ |
|
27
|
|
|
protected $shouldUseInlineStrings; |
|
28
|
|
|
|
|
29
|
|
|
/** @var \Box\Spout\Writer\XLSX\Helper\FileSystemHelper Helper to perform file system operations */ |
|
30
|
|
|
protected $fileSystemHelper; |
|
31
|
|
|
|
|
32
|
|
|
/** @var \Box\Spout\Writer\XLSX\Helper\SharedStringsHelper Helper to write shared strings */ |
|
33
|
|
|
protected $sharedStringsHelper; |
|
34
|
|
|
|
|
35
|
|
|
/** @var \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to apply styles */ |
|
36
|
|
|
protected $styleHelper; |
|
37
|
|
|
|
|
38
|
|
|
/** @var array contain column width information */ |
|
39
|
|
|
protected $columnwidths = array(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $tempFolder |
|
43
|
|
|
* @param bool $shouldUseInlineStrings |
|
44
|
|
|
* @param bool $shouldCreateNewSheetsAutomatically |
|
45
|
96 |
|
* @param \Box\Spout\Writer\Style\Style $defaultRowStyle |
|
46
|
|
|
* @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders |
|
47
|
96 |
|
*/ |
|
48
|
|
|
public function __construct($tempFolder, $shouldUseInlineStrings, $shouldCreateNewSheetsAutomatically, $defaultRowStyle, $columnwidths) |
|
49
|
96 |
|
{ |
|
50
|
|
|
parent::__construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle); |
|
51
|
96 |
|
|
|
52
|
96 |
|
$this->shouldUseInlineStrings = $shouldUseInlineStrings; |
|
53
|
|
|
|
|
54
|
96 |
|
$this->fileSystemHelper = new FileSystemHelper($tempFolder); |
|
55
|
|
|
$this->fileSystemHelper->createBaseFilesAndFolders(); |
|
56
|
|
|
|
|
57
|
96 |
|
$this->styleHelper = new StyleHelper($defaultRowStyle); |
|
58
|
96 |
|
|
|
59
|
96 |
|
// This helper will be shared by all sheets |
|
60
|
|
|
$xlFolder = $this->fileSystemHelper->getXlFolder(); |
|
61
|
|
|
$this->sharedStringsHelper = new SharedStringsHelper($xlFolder); |
|
62
|
|
|
|
|
63
|
|
|
$this->columnwidths = $columnwidths; |
|
64
|
60 |
|
} |
|
65
|
|
|
|
|
66
|
60 |
|
/** |
|
67
|
|
|
* @return \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to apply styles to XLSX files |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function getStyleHelper() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->styleHelper; |
|
72
|
60 |
|
} |
|
73
|
|
|
|
|
74
|
60 |
|
/** |
|
75
|
|
|
* @return int Maximum number of rows/columns a sheet can contain |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getMaxRowsPerWorksheet() |
|
78
|
|
|
{ |
|
79
|
|
|
return self::$maxRowsPerWorksheet; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
96 |
|
* Creates a new sheet in the workbook. The current sheet remains unchanged. |
|
84
|
|
|
* |
|
85
|
96 |
|
* @return Worksheet The created sheet |
|
86
|
96 |
|
* @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing |
|
87
|
|
|
*/ |
|
88
|
96 |
|
public function addNewSheet() |
|
89
|
96 |
|
{ |
|
90
|
96 |
|
$newSheetIndex = count($this->worksheets); |
|
91
|
|
|
$sheet = new Sheet($newSheetIndex); |
|
92
|
96 |
|
|
|
93
|
|
|
$worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder(); |
|
94
|
|
|
$worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->shouldUseInlineStrings, $this->columnwidths); |
|
95
|
|
|
$this->worksheets[] = $worksheet; |
|
96
|
|
|
|
|
97
|
|
|
return $worksheet; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set column width for sheet that will be created |
|
102
|
|
|
* should only be called from the writer |
|
103
|
63 |
|
* |
|
104
|
|
|
* @param array $columnwidths |
|
105
|
|
|
*/ |
|
106
|
63 |
|
public function _setColumnWidth( $columnwidths ) { |
|
107
|
|
|
$this->columnwidths = $columnwidths; |
|
108
|
63 |
|
} |
|
109
|
63 |
|
|
|
110
|
63 |
|
/** |
|
111
|
|
|
* Closes the workbook and all its associated sheets. |
|
112
|
63 |
|
* All the necessary files are written to disk and zipped together to create the XLSX file. |
|
113
|
|
|
* All the temporary files are then deleted. |
|
114
|
|
|
* |
|
115
|
63 |
|
* @param resource $finalFilePointer Pointer to the XLSX that will be created |
|
116
|
63 |
|
* @return void |
|
117
|
63 |
|
*/ |
|
118
|
63 |
|
public function close($finalFilePointer) |
|
119
|
63 |
|
{ |
|
120
|
63 |
|
/** @var Worksheet[] $worksheets */ |
|
121
|
|
|
$worksheets = $this->worksheets; |
|
122
|
63 |
|
|
|
123
|
63 |
|
foreach ($worksheets as $worksheet) { |
|
124
|
|
|
$worksheet->close(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->sharedStringsHelper->close(); |
|
128
|
|
|
|
|
129
|
|
|
// Finish creating all the necessary files before zipping everything together |
|
130
|
63 |
|
$this->fileSystemHelper |
|
131
|
|
|
->createContentTypesFile($worksheets) |
|
132
|
63 |
|
->createWorkbookFile($worksheets) |
|
133
|
63 |
|
->createWorkbookRelsFile($worksheets) |
|
134
|
63 |
|
->createStylesFile($this->styleHelper) |
|
135
|
|
|
->zipRootFolderAndCopyToStream($finalFilePointer); |
|
136
|
|
|
|
|
137
|
|
|
$this->cleanupTempFolder(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Deletes the root folder created in the temp folder and all its contents. |
|
142
|
|
|
* |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function cleanupTempFolder() |
|
146
|
|
|
{ |
|
147
|
|
|
$xlsxRootFolder = $this->fileSystemHelper->getRootFolder(); |
|
148
|
|
|
$this->fileSystemHelper->deleteFolderRecursively($xlsxRootFolder); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|