Priti's Technical Corner

April 8, 2014

Download Excel Sheet in Zf2

Filed under: PHP, Zend framework, ZF2 — Tags: , — pritisolanki @ 5:05 pm

 

$response = new \Zend\Http\Response\Stream();
$response->setStream(fopen($myfile, ‘r’)); //read file which you want to download as xls
$response->setStatusCode(200);

$headers = new \Zend\Http\Headers();
$headers->addHeaderLine(“Content-Type: application/vnd.ms-excel; charset=UTF-8”)
->addHeaderLine(‘Content-Disposition’, ‘attachment; filename=my.xls’)
->addHeaderLine( “Content-Transfer-Encoding: binary” )
->addHeaderLine(‘Content-Length’, filesize($myfile));
$response->setHeaders($headers);

There are some cases when you will see extra row in downloaded sheet in header section. To remove those extra empty lines in the downloaded sheet add ob_end_clean();  In start of above code.

Hope this little trick help some of the needy one !

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: