-
-
Save nebiros/288725 to your computer and use it in GitHub Desktop.
<?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(); | |
} | |
} |
Not working for me
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 = "'$str";
}
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
Its is giving me "Action Helper by name Layout not found" error? any solution?
I am new to Zend Framwork 1.2.
It generates excel file but when open directely it generates non english content like damage file.what i should do?
Thanks for the code, I think deleting this temporary file will be a good idea, before we exit.