Skip to content

Instantly share code, notes, and snippets.

@ZhukV
Created July 26, 2013 08:46
Show Gist options
  • Save ZhukV/6087320 to your computer and use it in GitHub Desktop.
Save ZhukV/6087320 to your computer and use it in GitHub Desktop.
Excel file response for download excel files usage PHPExcel library (Symfony HttpFoundation)
<?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;
}
}
@floriankapaun
Copy link

I've updated this snippet: Updated XlsxResponse.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment