I'm using PHPExcel for exporting a report.
It's gave me an error when I run it on internet (using PHP 5.6)..
But, when I'm test on my localhost, it's fine. Works perfectly (using 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');
code. It's asking for saving the excel, but the excel file can't be open because it's corrupted..
- 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. ERR_INVALID_RESPONSE
- UPDATE
- I try to add
ob_end_clean();
orob_clean();
before$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". - I try to change the
xlsx
extension toxls
onfilename
properties, and the Excel now can be opened. But It shows php errorMessage: ob_end_clean(): failed to delete buffer. No buffer to delete
- I try to keep the extension but I deleted the
ob_end_clean();
, and the error comes again.. - Solution :
- I changed the code to be
Excel5
like this
$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
5条答案
按热度按时间rvpgvaaj1#
我找到了一个修复程序,转到
Classes/PHPExcel/Writer/Excel2007.php
注解掉以下几行,php 7有一个wierd返回日期类型,这样做将使php excel在php 7中立即工作。至
对我有用。
ccrfmcuu2#
尝试安装ZipArchive类,即
sudo apt-get install php7.0-zip
因为这可能是新形成的服务器的问题。(对我来说是)
jpfvwuh43#
将
$writer
输出保存到一个文件并重定向到该文件解决了我的问题我还将时间限制和内存限制设置得很高,这样它们就不会引起任何问题
编码:
j7dteeu84#
添加ob_end_clean()
fv2wmkja5#
我有同样的问题与4000+行。增加内存限制和时间限制修复了这个问题。
ERR_INVALID_RESPONSE行为的原因是您发送了xls/x标头,但随后由于内存或时间不足而出现http错误500。