Created
July 26, 2013 08:46
-
-
Save ZhukV/6087320 to your computer and use it in GitHub Desktop.
Excel file response for download excel files usage PHPExcel library (Symfony HttpFoundation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\HttpFoundation; | |
use Symfony\Component\HttpFoundation\Response; | |
/** | |
* Response for download excel file | |
*/ | |
class ExcelFileResponse extends Response | |
{ | |
/** | |
* @var \PHPExcel_Writer_IWriter | |
*/ | |
private $writer; | |
/** | |
* @var string $content | |
*/ | |
private $excelContent; | |
/** | |
* Construct | |
* | |
* @param \PHPExcel_Writer_IWriter $writer | |
* @param string $fileName | |
*/ | |
public function __construct(\PHPExcel_Writer_IWriter $writer, $fileName) | |
{ | |
$this->writer = $writer; | |
ob_start(); | |
$writer->save('php://output'); | |
$this->excelContent = ob_get_contents(); | |
ob_end_clean(); | |
parent::__construct(null, 200, array( | |
'Content-Disposition' => sprintf('attachment; filename="%s"', $fileName), | |
'Content-Length' => mb_strlen($this->excelContent), | |
'Content-Type' => 'application/octet-stream; charset=utf-8' | |
)); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function sendContent() | |
{ | |
print $this->excelContent; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've updated this snippet: Updated
XlsxResponse.php