[英]PHPExcel error while saving
I'm using PHPExcel for exporting a report.我正在使用 PHPExcel 导出报告。
It's gave me an error when I run it on internet (using PHP 5.6)..当我在互联网上运行它时它给了我一个错误(使用 PHP 5.6)。
But, when I'm test on my localhost, it's fine.但是,当我在本地主机上进行测试时,一切正常。 Works perfectly (using PHP 5.4.31)
完美运行(使用 PHP 5.4.31)
Here's my code这是我的代码
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');
}
the weird things happen when I remove the $objWriter->save('php://output');
当我删除
$objWriter->save('php://output');
时,奇怪的事情发生了$objWriter->save('php://output');
code.代码。 It's asking for saving the excel, but the excel file can't be open because it's corrupted..
它要求保存excel,但excel文件无法打开,因为它已损坏..
*EDIT *编辑
The error is错误是
This site can't be reached The webpage at http://my-link-in-here might be temporarily down or it may have moved permanently to a new web address.无法访问此站点http://my-link-in-here 上的网页可能暂时关闭,或者它可能已永久移至新网址。 ERR_INVALID_RESPONSE
ERR_INVALID_RESPONSE
*UPDATE *更新
I try to add ob_end_clean();
我尝试添加
ob_end_clean();
or ob_clean();
或
ob_clean();
before $objWriter->save('php://output');
$objWriter->save('php://output');
and it saving the Excel, but I can't open it because Excel says "File Format or File Extention is not valid".它保存了 Excel,但我无法打开它,因为 Excel 显示“文件格式或文件扩展名无效”。
I try to change the xlsx
extension to xls
on filename
properties, and the Excel now can be opened.我尝试将
filename
属性上的xlsx
扩展名更改为xls
,现在可以打开 Excel。 But It shows php error Message: ob_end_clean(): failed to delete buffer. No buffer to delete
但它显示 php 错误
Message: ob_end_clean(): failed to delete buffer. No buffer to delete
Message: ob_end_clean(): failed to delete buffer. No buffer to delete
I try to keep the extension but I deleted the ob_end_clean();
我尝试保留扩展名,但我删除了
ob_end_clean();
, and the error comes again.. ,错误又来了..
*Solution : *解决方案 :
I changed the code to be Excel5
like this我把代码改成了这样的
Excel5
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="userList.xls"');
And it works like champion.它像冠军一样工作。 Anyone can make a explanation ?
任何人都可以做出解释吗? I will give the solution to the one who can make the explanation
我会把解决方案给能解释的人
I found a fix,go to Classes/PHPExcel/Writer/Excel2007.php
comment out the following lines, php 7 has a wierd return date type so doing this will enable php excel to work in php 7 pronto.我找到了一个修复程序,转到
Classes/PHPExcel/Writer/Excel2007.php
注释掉以下几行,php 7 有一个奇怪的返回日期类型,所以这样做将使 php excel 能够在 php 7 pronto 中工作。 any way反正
$saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
to到
//$saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
//PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
//PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
worked for me.为我工作。
Try install ZipArchive Class ie sudo apt-get install php7.0-zip
尝试安装 ZipArchive 类,即
sudo apt-get install php7.0-zip
as it might be the problem for newly formed server.(It was for me)因为这可能是新组建的服务器的问题。(这是给我的)
Add ob_end_clean()添加 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;
I had same problem with 4000+ rows.我对 4000 多行有同样的问题。 Increasing both memory limit and time limit fixed the problem.
增加内存限制和时间限制解决了这个问题。
Reason for ERR_INVALID_RESPONSE behavior is that you send xls/x headers but then http error 500 from running out of memory or time. 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;
Saving the $writer
output to a file and redirecting to it solved my problem将
$writer
输出保存到文件并重定向到它解决了我的问题
I also set the time limit and memory limit high so that they don't cause any problem我还将时间限制和内存限制设置得很高,以免造成任何问题
Code:代码:
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.