diff --git a/README.md b/README.md index 1cd13fb..10da766 100644 --- a/README.md +++ b/README.md @@ -93,4 +93,3 @@ Remaining todos include: * Tests and test coverage * TravisCI * Improved documentation -* `createStreamedResponse()` \ No newline at end of file diff --git a/src/Factory.php b/src/Factory.php index 66d80dd..b6d536f 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -4,6 +4,7 @@ use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; +use Symfony\Component\HttpFoundation\StreamedResponse; /** * Factory class for PhpSpreadsheet objects. @@ -56,4 +57,27 @@ public function createReader($type) return new $readerClass(); } + + /** + * Return a StreamedResponse containing the file + * + * @param Spreadsheet $spreadsheet + * @param unknown $type + * @param number $status + * @param array $headers + * @return \Symfony\Component\HttpFoundation\StreamedResponse + */ + public function createStreamedResponse(Spreadsheet $spreadsheet, $type, $status = 200, $headers = array()) + { + $writer = IOFactory::createWriter($spreadsheet, $type); + + return new StreamedResponse( + function () use ($writer) { + $writer->save('php://output'); + }, + $status, + $headers + ); + } + } \ No newline at end of file