Skip to content

Commit 047298c

Browse files
Added functions download and downloadCsv.
1 parent ecb2b81 commit 047298c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,5 @@ Output:
6767
* streamAsCsv.
6868
* save.
6969
* createSheet.
70+
* download($fileName | 'file.xlsx').
71+
* downloadAsCsv($fileName | 'file.csv').

src/Services/PhpSpreadsheetExcelService.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,38 @@ public function save($path)
188188
return true;
189189
}
190190

191+
public function download($fileName = 'file.xlsx')
192+
{
193+
$writer = new XlsxWriter($this->spreadsheet);
194+
195+
$headers = [
196+
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
197+
'Content-Disposition' => 'attachment; filename="'. $fileName.'"'
198+
];
199+
200+
foreach ($headers as $key => $value) {
201+
header($key. ': '. $value);
202+
}
203+
204+
$writer->save('php://output');
205+
}
206+
207+
public function downloadAsCsv($fileName = 'file.csv')
208+
{
209+
$writer = new XlsxWriter($this->spreadsheet);
210+
211+
$headers = [
212+
'Content-Type' => 'text/csv',
213+
'Content-Disposition' => 'attachment; filename="'. $fileName.'"'
214+
];
215+
216+
foreach ($headers as $key => $value) {
217+
header($key. ': '. $value);
218+
}
219+
220+
$writer->save('php://output');
221+
}
222+
191223
private function convertNumberToLetter($number)
192224
{
193225
$startFrom = 'A';

0 commit comments

Comments
 (0)