I am trying to generate an excel file with the extension .xlsx from the code below. I am able to download the file very well but when I open it with excel sheet, I receive the following warning error .. Excel cannot open the file 'dindi.xlsx' because the file format or file extension is not valid. Ensure that the file is not corrupted and that the file extension matches the format of the file. When I opened it with notepad ,the file had the following error:
Notice: ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete
Below is the codeof what I'm trying to do.
public function exportResults() { $this -> load -> database(); $query = $this -> db -> query(" SELECT * FROM farm LIMIT 10"); $results = $query -> result_array(); $objPHPExcel = new Excel(); $objReader = PHPExcel_IOFactory::createReader('Excel2007'); $objPHPExcel = $objReader->load('./files/farmdetails.xlsx'); $objPHPExcel ->getActiveSheet()->setTitle('farmreport'); $objPHPExcel -> setActiveSheetIndex(0); $i = 1; foreach ($results as $result) { $objPHPExcel -> getActiveSheet() -> SetCellValue('A' . $i, $result["name"]); $objPHPExcel -> getActiveSheet() -> SetCellValue('B' . $i, $result["dateofcontract"]); $objPHPExcel -> getActiveSheet() -> SetCellValue('C' . $i, $result["leasorname"]); $objPHPExcel -> getActiveSheet() -> SetCellValue('D' . $i, $result["acre"]); $objPHPExcel -> getActiveSheet() -> SetCellValue('E' . $i, $result["zone"]); $i++; echo $result["name"]; } ob_end_clean(); $filename = "dindi.xlsx"; header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename=' . $filename); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); ob_end_clean(); $objWriter -> save('php://output'); $objPHPExcel -> disconnectWorksheets(); unset($objPHPExcel); }
Add this piece of code. Happens on live site when minifyhtml is active. Can be connected to advagg and httprl requests.
Proposed fix is to wrap ob_clean()
with ob_get_length()
condition.
if(ob_get_length() > 0) { ob_clean(); }
That error is just telling you that there was no buffer to delete. To avoid it just use:
if (ob_get_contents()) ob_end_clean();
(check if there's an active output buffer) or:
if (ob_get_length()) ob_end_clean();
(checks if there's a non empty string in the buffer) as suggested by @Venu.
also you are calling ob_end_clean();
two times there. And that only works with stackable buffers. From the PHP manual:
This function discards the contents of the topmost output buffer and turns off this output buffering.
Are you sure you don't want to just use ob_clean()?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With