生成并下载Excel文件会生成ERR_INVALID_RESPONSE [英] Generating and downloading an excel file generates a ERR_INVALID_RESPONSE

查看:85
本文介绍了生成并下载Excel文件会生成ERR_INVALID_RESPONSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

 public function downloadexcel($requestId) {
    $this->loadModel('MaterialsRequest');
    $materialsRequests = $this->MaterialsRequest->find('all', array('conditions' => array('MaterialsRequest.request_id' => $requestId)));
    date_default_timezone_set('Europe/London');

    if (PHP_SAPI == 'cli')
        die('This example should only be run from a Web Browser');

    /** Include PHPExcel */
    //require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
    // Create new PHPExcel object
    $objPHPExcel = new PHPExcel();

    // Set document properties
    $objPHPExcel->getProperties()->setCreator("mez")
            ->setLastModifiedBy("mez")
            ->setTitle("Supply Template")
            ->setSubject("Supply Template")
            ->setDescription("Supply Template , please fill and upload.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("Supply");




    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
    $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
    $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER);

    // Add some data
    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Material ID')
            ->setCellValue('B1', 'Material Name')
            ->setCellValue('C1', 'Quantity')
            ->setCellValue('D1', 'Your offer');


    $index = 2;
    foreach ($materialsRequests as $oneMaterialsRequests) {

        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A' . $index, $oneMaterialsRequests['MaterialsRequest']['material_id'])
                ->setCellValue('B' . $index, $oneMaterialsRequests['Material']['name'])
                ->setCellValue('C' . $index, $oneMaterialsRequests['MaterialsRequest']['qty'])
                ->setCellValue('E' . $index, $oneMaterialsRequests['MaterialsRequest']['id']);


        $index++;
    }

    /// Set sheet security
    $MaterialsRequestsNo = count($materialsRequests);
    $MaterialsRequestsNo+=1; //add count for header
    $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
    $objPHPExcel->getActiveSheet()
            ->getStyle('D2:D' . $MaterialsRequestsNo)
            ->getProtection()->setLocked(
            PHPExcel_Style_Protection::PROTECTION_UNPROTECTED
    );


    // Miscellaneous glyphs, UTF-8
    // Rename worksheet
    $objPHPExcel->getActiveSheet()->setTitle('Supply');


    // 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-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="Supply.xlsx"');
    header('Cache-Control: max-age=0');
    // If you're serving to IE 9, then the following may be needed
    header('Cache-Control: max-age=1');

    // If you're serving to IE over SSL, then the following may be needed
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
    header('Cache-Control: cache, must-revalidate');// HTTP/1.1
    header('Pragma: public'); // HTTP/1.0

    $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
    $objWriter->save('php://output');

    exit;
}

当我调用此函数时,从我的本地服务器上点亮时,我可以获得excel工作表,并且在调用该函数后,网页将关闭,但是当我在apache服务器上调用它时,它会引发错误: ERR_INVALID_RESPONSE ,我经历了放置die();的代码;在每行之后查看其响应方式,直到在 $ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,'Excel2007');之后放置die(); 它将下载电子表格,但是它将已损坏,无法关闭页面,也不会显示该错误消息,但是显然 $ objWriter-> save('php://output'); 会导致错误向上,页面将冻结,并且无法下载excel文件.

From my local server operating on lamp when I call this function I can get the excel sheet and the web page would close after I call the function, when I'm calling it however on my apache server it brings out the error: ERR_INVALID_RESPONSE, I went through the code putting a die(); after each line to see how it would respond and up until putting a die() after $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007'); it would download the spreadsheet but it would be corrupted and wouldn't close the page and it wouldn't bring up that error message, however apparently $objWriter->save('php://output'); is causing the error to come up, the page would freeze and the excel file wouldn't get downloaded.

我正在调试其他人的代码,但我无法弄清楚这部分.

I debugging someone else's code but I can't figure this part out.

推荐答案

最终,问题出在ZipArchive类中,这就是为什么它在一台服务器上起作用而在另一台服务器上不起作用的原因.

Eventually the issue was in the ZipArchive class, that's why it worked on one server and didn't work on the other.

此处有更多文档: http://www.php.net/manual/zh/zip.installation.php

这篇关于生成并下载Excel文件会生成ERR_INVALID_RESPONSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆