保存时出现PHPExcel错误

时间:2017-02-02 04:10:15

标签: php phpexcel

我正在使用PHPExcel导出报告。

当我在互联网上运行它时(使用PHP 5.6),它给了我一个错误。

但是,当我在我的localhost上测试时,它很好。完美运行(使用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

* UPDATE

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

  • 我尝试将xlsx扩展程序更改为xls属性上的filename,现在可以打开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"');
    

    它就像冠军一样。有人可以解释一下吗?我会给那些可以解释的人解决方案

5 个答案:

答案 0 :(得分:2)

我找到了一个修复程序,转到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);

为我工作。

答案 1 :(得分:1)

添加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;

答案 2 :(得分: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

答案 3 :(得分:0)

我有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;

答案 4 :(得分:0)

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

因为它可能是新组建的服务器的问题。(这对我而言)