【问题标题】:Generating and downloading an excel file generates a ERR_INVALID_RESPONSE生成和下载 excel 文件会生成 ERR_INVALID_RESPONSE
【发布时间】:2015-10-01 18:46:55
【问题描述】:

这是我的代码:

 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 文件。

我调试了别人的代码,但我无法弄清楚这部分。

【问题讨论】:

标签: php excel


【解决方案1】:

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

更多文档在这里:http://www.php.net/manual/en/zip.installation.php

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。使用“php -m”检查它会为您提供模块列表并检查您是否安装了“zip”模块。 http://php.net/manual/en/book.zip.php

    PhpExcel 需要此模块。如果您没有,请尝试安装或更改您的 php 版本。就我而言,我解决了将我的 php 从 7 更改为 php 5.5

    【讨论】:

    • 我建议您启用 Excel Lib 的所有要求: PHP 扩展 php_zip 已启用 PHP 扩展 php_xml 已启用 PHP 扩展 php_gd2 已启用 注意:不要忘记重新启动服务器。
    【解决方案3】:

    您可以尝试保存到临时文件并使用重定向来提供它:

    $objWriter->save('wwwroot/some-file.xlsx');
    header("Location: /some-file.xlsx");
    exit();
    

    【讨论】:

    • 感谢您的回答,我在代码末尾添加了这个而不是 $objWriter->save('php://output'),但它仍然给了我相同的结果。
    【解决方案4】:

    现在使用 php 7.4 你必须使用 --with-zip 选项

    【讨论】:

      【解决方案5】:

      在 php 7.4 中

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-08
        • 2014-12-17
        • 1970-01-01
        • 1970-01-01
        • 2018-12-31
        • 2011-04-11
        相关资源
        最近更新 更多