Skip to content

Instantly share code, notes, and snippets.

@nebiros
Created January 28, 2010 13:13
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nebiros/288725 to your computer and use it in GitHub Desktop.
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();
}
}
Copy link

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
Copy link

Harrrrry commented Mar 3, 2016

Not working for me

@saibal-roy
Copy link

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
Copy link

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

@jitendra2
Copy link

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