I am studying PHPExcel and I did one of its example.
I did like this:
public function generateExcelTemplate($quotation_id) {
//error reporting
error_reporting(E_ALL);
//include path
ini_set('include_path', ini_get('include_path').';../Classes/');
//php excel
include('application/libraries/PHPExcel.php');
//php excel writer 2007
include('application/libraries/PHPExcel/Writer/Excel2007.php');
//create new phpexcel object
echo date('H:i:s') . " Create new PHPExcel object \n";
$objPHPExcel = new PHPExcel();
//set properties
echo date('H:i:s') . " Set properties \n";
$objPHPExcel->getProperties()->setCreator('Rochelle Canale');
$objPHPExcel->getProperties()->setLastModifiedBy('Rochelle Canale');
$objPHPExcel->getProperties()->setTitle('My New Excel');
$objPHPExcel->getProperties()->setSubject('My New Excel Subject');
$objPHPExcel->getProperties()->setDescription('test document');
//add some data
echo date('H:i:s') . " Add some data \n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'World');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'World');
//rename sheet
echo date('H:i:s') . " Rename sheet\n";
$objPHPExcel->getActiveSheet()->setTitle('Simple');
//save excel 2007
echo date('H:i:s') . " Write to excel2007 format \n";
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace('.php','.xlsx', __FILE__));
//ECHO DONE
echo date('H:i:s') . " Done writing \r\n";
}
And it looks fine because it actually generate the excel. My problem is there's no dialog for saving the file. When I check the demo and when you click the link it will prompt a confirmation box asking if you want to save or open. In my page when I load it it simple shows the text.
I think after all of your above code listing try putting the following code lines:
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel2007)
//clean the output buffer
ob_end_clean();
//this is the header given from PHPExcel examples. but the output seems somewhat corrupted in some cases.
//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//so, we use this header instead.
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$properties_arr['excel_file_name'].'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
Note:
I assume that your above code listing is already generates a good readable Excel sheet, but you are unable to make it downloadable. My above code snippet will get done that for you!
And within CodeIgniter's config directory there is a mimes.php and you need to add/edit the line for the xlsx
which is an associative key-value pair for $mimes
array. Value for the key xlsx
should be as follows:
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/vnd.ms-excel'),
Hope this works!
If it only shows the text, your code is understood and served as html instead of php code.
Every php script needs to start with <?php
eg
<?php
echo "Hello World: ".date("Y-m-d H:i");
?>
Also the file should be named *.php
eg index.php
If that doesn't help make sure you installed php in your webserver, eg apt-get install libapache2-mod-php5
when you're on linux with apache.
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.