生成和下载excel文件会生成ERR_INVALID_RESPONSE

e0bqpujr  于 2023-01-18  发布在  其他
关注(0)|答案(5)|浏览(232)

这是我的代码:

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

当我调用这个函数时,我可以从运行在lamp上的本地服务器获得excel工作表,并且调用该函数后网页会关闭,但是当我在Apache服务器上调用它时,它会带来错误:ERR_INVALID_RESPONSE,我检查了代码,放入了一个骰子();在每行之后查看它将如何响应,直到在$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');之后放置一个die(),它将下载电子表格,但它将被损坏,不会关闭页面,也不会显示该错误消息,但显然$objWriter->save('php://output');导致错误出现,页面将冻结,Excel文件不会下载。
我在调试别人的代码,但我搞不懂这部分。

fnvucqvd

fnvucqvd1#

最终问题出在ZipArchive类中,这就是为什么它在一个服务器上工作,而在另一个服务器上不工作。
更多文档请访问:http://www.php.net/manual/en/zip.installation.php

5jdjgkvh

5jdjgkvh2#

我也遇到了同样的问题。用“php -m”检查,它会给你一个模块列表,并检查你是否安装了“zip”模块。http://php.net/manual/en/book.zip.php
这个模块是需要的PhpExcel。如果你没有它,尝试安装或改变你的php版本。在我的情况下,我解决了改变我的php从7到php 5. 5

cidc1ykv

cidc1ykv3#

您可以尝试保存到临时文件,并与重定向服务代替:

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

dojqjjoe4#

现在在php7.4中你必须使用--with-zip选项

ufj5ltwl

ufj5ltwl5#

在php7.4中

//$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save('php://output');

exit;

相关问题