Skip to content

Commit 6051a6b

Browse files
committed
Issue #10 Allow for sheet creation without saving
1 parent 2307cf6 commit 6051a6b

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Methods | Description
5555
---------|-------------
5656
`saveAs($name)` | Saves the excel file under `$name`
5757
`send($name=null,$inline=false)` | Sends the excel file to the browser. If `$name` is empty, the file is streamed for inline display, otherwhise a download dialog will open, unless `$inline` is `true` which will force inline display even if a filename is supplied.
58+
`createSheets()` | Only creates the sheets of the excel workbook but does not save the file. This is usually called implicitely on `saveAs()` and `send()` but can also be called manually to modify the sheets before saving.
5859
`getWriter()` | Returns the `PHPExcel_Writer_Abstract` instance
5960
`getWorkbook()` | Returns the `PHPExcel` workbook instance
6061
`getTmpFile()` | Returns the `mikehaertl\tmp\File` instance of the temporary file

src/ExcelFile.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class ExcelFile extends Object
2626
protected $_workbook;
2727
protected $_sheets;
2828
protected $_tmpFile;
29-
protected $_created = false;
29+
protected $_fileCreated = false;
30+
protected $_sheetsCreated = false;
3031

3132
/**
3233
* @return PHPExcel_Writer_Abstract the writer instance
@@ -108,11 +109,11 @@ public function send($filename = null, $inline = false)
108109
}
109110

110111
/**
111-
* Create the Excel file and save it to the temp file
112+
* Create the Excel sheets if they were not created yet
112113
*/
113-
protected function createFile()
114+
public function createSheets()
114115
{
115-
if (!$this->_created) {
116+
if (!$this->_sheetsCreated) {
116117
$workbook = $this->getWorkbook();
117118
$i = 0;
118119
foreach ($this->sheets as $title => $config) {
@@ -131,8 +132,19 @@ protected function createFile()
131132
}
132133
Yii::createObject($config, [$sheet])->render();
133134
}
135+
$this->_sheetsCreated = true;
136+
}
137+
}
138+
139+
/**
140+
* Create the Excel file and save it to the temp file
141+
*/
142+
protected function createFile()
143+
{
144+
if (!$this->_fileCreated) {
145+
$this->createSheets();
134146
$this->getWriter()->save((string) $this->getTmpFile());
135-
$this->_created = true;
147+
$this->_fileCreated = true;
136148
}
137149
}
138150
}

0 commit comments

Comments
 (0)