phpexcel - 下载文件已损坏

时间:2017-06-30 14:02:30

标签: php excel phpexcel

我正在尝试下载phpexcel生成的excel文件。我正在使用示例"简单下载xlsx" phpexcel

我使用this SO question中给出的相同概念。

我通过php来自客户端的请求调用POST文件。没有传递任何标题。使用以下代码:

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)} 

这里MessageService.invokePostRequest是ajax的包装器。

在服务器端,我在php代码下方

// 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;

在这里,我尝试在客户端和服务器上使用Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

我收到了响应标题(来自chrome&#39的开发人员工具):

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

content-length我假设有一些数据,在响应标签中我可以看到一些乱码文本。但我认为,该响应没有正确转换为excel文件。

一个文件总是被下载,当我打开文件时,它在0和#34的JSON中有文本"意外的标记P。

我不知道我在这里做错了什么。 如何下载phpexcel生成的正确Excel文件?

1 个答案:

答案 0 :(得分:1)

尝试使用其他标题,它适用于我。并尝试通过PHP打开没有js。如果php打开正确,请尝试在js脚本中找到错误。

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');