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