| 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 |
/** |
| 39 |
* @param string $tempFolder |
| 40 |
* @param bool $shouldUseInlineStrings |
| 41 |
* @param bool $shouldCreateNewSheetsAutomatically |
| 42 |
* @param \Box\Spout\Writer\Style\Style $defaultRowStyle |
| 43 |
* @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders |
| 44 |
*/ |
| 45 |
public function __construct($tempFolder, $shouldUseInlineStrings, $shouldCreateNewSheetsAutomatically, $defaultRowStyle) |
| 46 |
{ |
| 47 |
parent::__construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle); |
| 48 |
|
| 49 |
$this->shouldUseInlineStrings = $shouldUseInlineStrings; |
| 50 |
|
| 51 |
$this->fileSystemHelper = new FileSystemHelper($tempFolder); |
| 52 |
$this->fileSystemHelper->createBaseFilesAndFolders(); |
| 53 |
|
| 54 |
$this->styleHelper = new StyleHelper($defaultRowStyle); |
| 55 |
|
| 56 |
// This helper will be shared by all sheets |
| 57 |
$xlFolder = $this->fileSystemHelper->getXlFolder(); |
| 58 |
$this->sharedStringsHelper = new SharedStringsHelper($xlFolder); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @return \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to apply styles to XLSX files |
| 63 |
*/ |
| 64 |
protected function getStyleHelper() |
| 65 |
{ |
| 66 |
return $this->styleHelper; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @return int Maximum number of rows/columns a sheet can contain |
| 71 |
*/ |
| 72 |
protected function getMaxRowsPerWorksheet() |
| 73 |
{ |
| 74 |
return self::$maxRowsPerWorksheet; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Creates a new sheet in the workbook. The current sheet remains unchanged. |
| 79 |
* |
| 80 |
* @return Worksheet The created sheet |
| 81 |
* @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing |
| 82 |
*/ |
| 83 |
public function addNewSheet() |
| 84 |
{ |
| 85 |
$newSheetIndex = count($this->worksheets); |
| 86 |
$sheet = new Sheet($newSheetIndex, $this->internalId); |
| 87 |
|
| 88 |
$worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder(); |
| 89 |
$worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->styleHelper, $this->shouldUseInlineStrings); |
| 90 |
$this->worksheets[] = $worksheet; |
| 91 |
|
| 92 |
return $worksheet; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Closes the workbook and all its associated sheets. |
| 97 |
* All the necessary files are written to disk and zipped together to create the XLSX file. |
| 98 |
* All the temporary files are then deleted. |
| 99 |
* |
| 100 |
* @param resource $finalFilePointer Pointer to the XLSX that will be created |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function close($finalFilePointer) |
| 104 |
{ |
| 105 |
/** @var Worksheet[] $worksheets */ |
| 106 |
$worksheets = $this->worksheets; |
| 107 |
|
| 108 |
foreach ($worksheets as $worksheet) { |
| 109 |
$worksheet->close(); |
| 110 |
} |
| 111 |
|
| 112 |
$this->sharedStringsHelper->close(); |
| 113 |
|
| 114 |
// Finish creating all the necessary files before zipping everything together |
| 115 |
$this->fileSystemHelper |
| 116 |
->createContentTypesFile($worksheets) |
| 117 |
->createWorkbookFile($worksheets) |
| 118 |
->createWorkbookRelsFile($worksheets) |
| 119 |
->createStylesFile($this->styleHelper) |
| 120 |
->zipRootFolderAndCopyToStream($finalFilePointer); |
| 121 |
|
| 122 |
$this->cleanupTempFolder(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Deletes the root folder created in the temp folder and all its contents. |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
protected function cleanupTempFolder() |
| 131 |
{ |
| 132 |
$xlsxRootFolder = $this->fileSystemHelper->getRootFolder(); |
| 133 |
$this->fileSystemHelper->deleteFolderRecursively($xlsxRootFolder); |
| 134 |
} |
| 135 |
} |
| 136 |
|