Create a gist now

Instantly share code, notes, and snippets.

Embed
Export data to excel on Zend Framework
<?php
class IndexController extends Zend_Controller_Action
{
public function exportXlsAction()
{
set_time_limit( 0 );
$model = new Default_Model_SomeModel();
$data = $model->getData();
$filename = APPLICATION_PATH . "/tmp/excel-" . date( "m-d-Y" ) . ".xls";
$realPath = realpath( $filename );
if ( false === $realPath )
{
touch( $filename );
chmod( $filename, 0777 );
}
$filename = realpath( $filename );
$handle = fopen( $filename, "w" );
$finalData = array();
foreach ( $data AS $row )
{
$finalData[] = array(
utf8_decode( $row["col1"] ), // For chars with accents.
utf8_decode( $row["col2"] ),
utf8_decode( $row["col3"] ),
);
}
foreach ( $finalData AS $finalRow )
{
fputcsv( $handle, $finalRow, "\t" );
}
fclose( $handle );
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$this->getResponse()->setRawHeader( "Content-Type: application/vnd.ms-excel; charset=UTF-8" )
->setRawHeader( "Content-Disposition: attachment; filename=excel.xls" )
->setRawHeader( "Content-Transfer-Encoding: binary" )
->setRawHeader( "Expires: 0" )
->setRawHeader( "Cache-Control: must-revalidate, post-check=0, pre-check=0" )
->setRawHeader( "Pragma: public" )
->setRawHeader( "Content-Length: " . filesize( $filename ) )
->sendResponse();
readfile( $filename ); exit();
}
}
@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Sep 5, 2013

Thanks for the code, I think deleting this temporary file will be a good idea, before we exit.

readfile( $filename ); 
unlink($filename);
exit();

ghost commented Sep 5, 2013

Thanks for the code, I think deleting this temporary file will be a good idea, before we exit.

readfile( $filename ); 
unlink($filename);
exit();
@Harrrrry

This comment has been minimized.

Show comment
Hide comment
@Harrrrry

Harrrrry Mar 3, 2016

Not working for me

Harrrrry commented Mar 3, 2016

Not working for me

@sunnyroy21

This comment has been minimized.

Show comment
Hide comment
@sunnyroy21

sunnyroy21 Apr 21, 2016

Use this method in the your custom library that is being autoloader namespaces and then call in the zend controller method it will work fine:
static function exportCsv($csvName,$csv_array)
{
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
$data=$csv_array;
// $data = array(
// array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),
// array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),
// array("firstname" => "James", "lastname" => "Brown", "age" => 31),
// array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7),
// array("firstname" => "Michael", "lastname" => "Davis", "age" => 43),
// array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24),
// array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27)
// );
// filename for download
$filename = $csvName.".csv";
header("Content-Disposition: attachment; filename="$filename"");
header("Content-Type: text/csv");
$out = fopen("php://output", 'w');
$flag = false;
foreach($data as $row) {
if(!$flag) {
// display field/column names as first row
fputcsv($out, array_keys($row), ',', '"');
$flag = true;
}
array_walk($row, 'cleanData');
fputcsv($out, array_values($row), ',', '"');
}
fclose($out);
exit;
}
function cleanData(&$str)
{
if($str == 't') $str = 'TRUE';
if($str == 'f') $str = 'FALSE';
if(preg_match("/^0/", $str) || preg_match("/^+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
$str = "'$str";
}
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}

Use this method in the your custom library that is being autoloader namespaces and then call in the zend controller method it will work fine:
static function exportCsv($csvName,$csv_array)
{
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
$data=$csv_array;
// $data = array(
// array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),
// array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),
// array("firstname" => "James", "lastname" => "Brown", "age" => 31),
// array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7),
// array("firstname" => "Michael", "lastname" => "Davis", "age" => 43),
// array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24),
// array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27)
// );
// filename for download
$filename = $csvName.".csv";
header("Content-Disposition: attachment; filename="$filename"");
header("Content-Type: text/csv");
$out = fopen("php://output", 'w');
$flag = false;
foreach($data as $row) {
if(!$flag) {
// display field/column names as first row
fputcsv($out, array_keys($row), ',', '"');
$flag = true;
}
array_walk($row, 'cleanData');
fputcsv($out, array_values($row), ',', '"');
}
fclose($out);
exit;
}
function cleanData(&$str)
{
if($str == 't') $str = 'TRUE';
if($str == 'f') $str = 'FALSE';
if(preg_match("/^0/", $str) || preg_match("/^+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
$str = "'$str";
}
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}

@manish-wedigtech

This comment has been minimized.

Show comment
Hide comment
@manish-wedigtech

manish-wedigtech May 9, 2017

Its is giving me "Action Helper by name Layout not found" error? any solution?
I am new to Zend Framwork 1.2.

Its is giving me "Action Helper by name Layout not found" error? any solution?
I am new to Zend Framwork 1.2.

@jitendra2

This comment has been minimized.

Show comment
Hide comment
@jitendra2

jitendra2 Jul 18, 2017

It generates excel file but when open directely it generates non english content like damage file.what i should do?

It generates excel file but when open directely it generates non english content like damage file.what i should do?

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