【问题标题】:PHPExcel error while saving保存时PHPExcel错误
【发布时间】:2017-06-19 01:01:39
【问题描述】:

我正在使用 PHPExcel 导出报告。

当我在互联网上运行它时出现错误(使用 PHP 5.6)..

但是,当我在本地主机上进行测试时,这很好。完美运行(使用 PHP 5.4.31)

这是我的代码

function downloadExcelBrand($brand,$tglAwal,$tglAkhir)
{

    $this->load->library('php_excel');

    $objPHPExcel = new PHPExcel(); 
    // print_r($objPHPExcel);die();
    $objPHPExcel->setActiveSheetIndex(0);

    $objPHPExcel->getActiveSheet()->SetCellValue('A1', "Concept");
    $objPHPExcel->getActiveSheet()->SetCellValue('B1', $brand);

    $objPHPExcel->getActiveSheet()->SetCellValue('A2', "Period");
    $objPHPExcel->getActiveSheet()->SetCellValue('B2', $tglAwal." to ".$tglAkhir);

    $objPHPExcel->getActiveSheet()->SetCellValue('A5', "Boutique");
    $objPHPExcel->getActiveSheet()->SetCellValue('B5', "Q1");
    $objPHPExcel->getActiveSheet()->SetCellValue('C5', "Q2");
    $objPHPExcel->getActiveSheet()->SetCellValue('D5', "Q3");
    $objPHPExcel->getActiveSheet()->SetCellValue('E5', "Q4");
    $objPHPExcel->getActiveSheet()->SetCellValue('F5', "BBC Score");

    $data = $this->surveymodel->getDataLaporanBrand($brand,$tglAwal,$tglAkhir);
    // print_r($data);die();
    $i = 5;
    foreach ($data as $index=>$value) {
        // print_r($data[$index+1]);die();
        if( $index%4 == 0){
            $i++;
            // print_r($value["Score"]);print_r($value['TotalData']);die();
            $AvgQ1 = $value["Score"] / $value['TotalData'];
            // print_r($AvgQ1);die();
            $AvgQ2 = $data[$index+1]["Score"]/$data[$index+1]['TotalData'];
            $AVGQ3 = $data[$index+2]["Score"]/$data[$index+2]['TotalData'];
            $AvgQ4 = $data[$index+3]["Score"]/$data[$index+3]['TotalData'];
            $BSC = $AvgQ1+$AvgQ2+$AVGQ3+$AvgQ4;

            $objPHPExcel->getActiveSheet()->SetCellValue('A'.$i, $value["boutiqueID"]);
            $objPHPExcel->getActiveSheet()->SetCellValue('B'.$i, $AvgQ1);
            $objPHPExcel->getActiveSheet()->SetCellValue('C'.$i, $AvgQ2);
            $objPHPExcel->getActiveSheet()->SetCellValue('D'.$i, $AVGQ3);
            $objPHPExcel->getActiveSheet()->SetCellValue('E'.$i, $AvgQ4);
            $objPHPExcel->getActiveSheet()->SetCellValue('F'.$i, $BSC);
        }
    }

    // Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file
    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
    // Write the Excel file to filename some_excel_file.xlsx in the current directory
    header('Content-type: application/vnd.ms-excel');
    // It will be called file.xls
    header('Content-Disposition: attachment; filename="Report_By_Brand.xlsx"');
    // Write file to the browser
    $objWriter->save('php://output');
}

当我删除 $objWriter->save('php://output'); 代码时会发生奇怪的事情。它要求保存excel,但是excel文件由于损坏而无法打开..

*编辑

  • 错误是

    无法访问此网站 http://my-link-in-here 的网页可能暂时关闭,或者它可能已永久移动到新的网址。 ERR_INVALID_RESPONSE

*更新

  • 我尝试在$objWriter->save('php://output'); 之前添加ob_end_clean();ob_clean(); 并保存Excel,但我无法打开它,因为Excel 显示“文件格式或文件扩展名无效”。

  • 我尝试在filename 属性上将xlsx 扩展名更改为xls,现在可以打开Excel。但它显示php错误Message: ob_end_clean(): failed to delete buffer. No buffer to delete

  • 我尝试保留扩展名但我删除了ob_end_clean();,错误又来了..

*解决方案:

  • 我把代码改成Excel5这样

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="userList.xls"');
    

    它就像冠军一样工作。任何人都可以做出解释吗?我会把解决方案交给能解释的人

【问题讨论】:

  • 错误说明了什么?
  • 这里是错误@Beginner This site can’t be reached The webpage at http://the-link-in-here might be temporarily down or it may have moved permanently to a new web address. ERR_INVALID_RESPONSE
  • 我无法提供解释:但也许如果您查看日志文件,或者打开在文本编辑器中生成的文件以查看它是否包含任何错误消息,它可能会为您提供解释
  • 生成的文件打不开?我已将扩展名更改为 xls 并使用 excel 打开它。它显示 php 错误 Message: ob_end_clean(): failed to delete buffer. No buffer to delete @MarkBaker
  • 所以删除ob_end_clean()

标签: php phpexcel


【解决方案1】:

$writer 输出保存到文件并重定向到它解决了我的问题

我还设置了时间限制和内存限制,这样它们就不会造成任何问题

代码:

        $writer = new PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); // instantiate Xlsx
        $writer->setPreCalculateFormulas(false);
        //ob_end_clean();
        set_time_limit(500);
        ini_set('memory_limit', '-1');
        $writer->save($filename . ".xlsx"); // save the file to root of project
        redirect('/' . $filename . ".xlsx"); // redirect the user to the file

【讨论】:

    【解决方案2】:

    尝试安装 ZipArchive 类,即sudo apt-get install php7.0-zip

    因为这可能是新成立的服务器的问题。(这是给我的)

    【讨论】:

    • 应该是评论。
    • 这是上述问题的答案@wayneOS
    【解决方案3】:

    我找到了解决方法,转到Classes/PHPExcel/Writer/Excel2007.php 注释掉以下行,php 7 有一个奇怪的返回日期类型,所以这样做将使 php excel 能够在 php 7 中工作。任何方式

    $saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
    PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
    PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
    

    //$saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
    //PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
    //PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
    

    为我工作。

    【讨论】:

    • 感谢这对我有用。我在 php7 上,可能适用于以下版本 ob_end_clean 将起作用。
    • tnx,你拯救了我的夜晚 :-) 顺便说一句。我在 php7.2
    【解决方案4】:

    我在 4000 多行时遇到了同样的问题。增加内存限制和时间限制解决了这个问题。

    ERR_INVALID_RESPONSE 行为的原因是您发送 xls/x 标头,但由于内存或时间不足而导致 http 错误 500。

    set_time_limit(120);
    ini_set('memory_limit', '256M');
    ...
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    ob_end_clean();
    $objWriter->save('php://output');
    exit;
    

    【讨论】:

      【解决方案5】:

      添加 ob_end_clean()

      header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
          header('Content-Disposition: attachment;filename="Filename.xlsx"');
          header('Cache-Control: max-age=0');
          $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
          ob_end_clean();
          $objWriter->save('php://output');
          exit;
      

      【讨论】:

      • 添加 ob_end_clean() 正在消除错误并允许下载文件。但是下载的文件还是打不开。据说格式或文件扩展名无效
      • 如果你把header('Content-type: application/vnd.ms-excel');改成header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');会怎么样
      • 仍然无法打开文件。它仍然说同样的错误@Jordan Sipahutar imgur.com/a/ehsT6
      • 您需要确保您提供的文件扩展名与作者匹配,并且与内容类​​型标题匹配
      • 我在上面写的代码的标题属性上是否有任何错误的代码?? @MarkBaker ??我不明白为什么这是托管错误。因为在我的本地主机上它工作得很好
      猜你喜欢
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2015-02-07
      • 1970-01-01
      相关资源
      最近更新 更多