Reputation: 1999
I am trying to download excel file generated by phpexcel
. I am using example "Simple download xlsx" of phpexcel
.
I am using same concept given in this SO question.
I am calling php
file by POST
request from client. Not passing any header. Using below code:
MessageService.invokePostRequest(
"/rest/testphpexcel1.php",
function(returnedResponse){
var data_type = 'application/vnd.ms-excel';
var excelDoc = new Blob([returnedResponse], {
type: data_type
});
var url = window.URL.createObjectURL(excelDoc);
var exportLink = new core.Element('a');
exportLink.setAttribute('href', url);
exportLink.setAttribute('download', this.filename + '.xlsx');
exportLink.trigger('click');
//adding some delay in removing the dynamically created link solved the problem in FireFox
setTimeout(function() {
window.URL.revokeObjectURL(url);
}, 0);
}.bind(this),{data: JSON.stringify(postData)}
Here MessageService.invokePostRequest
is a wrapper around ajax.
On server side I've below php
code:
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// 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)
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
Here I tried with Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
on client and server both side.
I am getting below response headers (from chrome's developer tools):
cache-control:must-revalidate
connection:Keep-Alive
content-disposition:attachment;filename="01simple.xlsx"
content-length:6309
content-transfer-encoding:binary
content-type:text/html; charset=UTF-8
date:Fri, 30 Jun 2017 13:51:40 GMT
keep-alive:timeout=5, max=100
pragma:public
server:Apache/2.4.6 (CentOS) PHP/5.4.16
x-powered-by:PHP/5.4.16
From content-length
I assume there is some data and in response tab I can see some gibberish text. But that response is not properly converted to excel file, I think.
A file is always getting downloaded and when I open the file, it has text "Unexpected token P in JSON at position 0".
I don't know what am I doing wrong here.
How to download a proper excel file generated by phpexcel
?
Upvotes: 0
Views: 1803
Reputation: 229
Try to use additional headers, it works for me. And try to open by php without js. If php opens correct, try to find error in js script.
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition:attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
Upvotes: 1