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
xlsxextension toxlsonfilenameproperties, and the Excel now can be opened. But It shows php errorMessage: ob_end_clean(): failed to delete buffer. No buffer to deleteI try to keep the extension but I deleted the
ob_end_clean();, and the error comes again..
*Solution :
I changed the code to be
Excel5like 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
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- M Argus Chopin Gyverxlsand open it using excel. It shows php errorMessage: ob_end_clean(): failed to delete buffer. No buffer to delete@MarkBaker - M Argus Chopin Gyverob_end_clean()- Mark Baker