i have a problem with exporting an excel file from php
$filename = realpath($filename);
$handle = fopen($filename, "w");
$cols = array();
fputcsv($handle,$cols, "\t");
$table = array($_POST);
foreach ($table as $row){
fputcsv($handle, $row, "\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();
With this code I get an excel file, but the data are all in the column A(excel) How can I get a right format? The variable $table looks like:
//EDIT var_dump($_POST) looks like
<tr><th>Name</th><th>Surname</th></tr>
<tr><td>Mike</td><td>Dirnt</td></tr>
The problem here is that you are actually creating a CSV file, not an XLS file. They are very different things. Save it as .csv with the content-type set to text/csv instead:
$this->getResponse()->setRawHeader( "Content-Type: text/csv; charset=UTF-8" )
->setRawHeader( "Content-Disposition: attachment; filename=excel.csv" )
->setRawHeader(.......
It will still open in Excel!
Please follow up below code with your data it will work on zend. Also please replace with your controller class.
Class ExportExcelController extends Zend_Controller_Action{
public function exportexcelAction()
{
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 ); unlink($filename); exit();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.