问题描述
我试图从下面的代码生成一个扩展名为.xlsx的excel文件。我可以很好地下载文件,但当我打开它与Excel表,我收到以下警告错误。Excel无法打开文件'dindi.xlsx',因为文件格式或文件扩展名无效。确保文件未损坏,并且文件扩展名与文件的格式匹配。
当我用记事本打开它时,文件有以下错误:
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:
下面是我要做的代码。
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);
}
推荐答案
这个错误只是告诉你没有要删除的缓冲区。
为了避免它只使用:
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();
(检查缓冲区中是否有非空字符串)
@Venu建议。
(checks if there's a non empty string in the buffer)as suggested by @Venu.
也是你调用 ob_end_clean();
两次。这只适用于可堆叠缓冲区。从PHP手册:
also you are calling ob_end_clean();
two times there. And that only works with stackable buffers. From the PHP manual:
您确定不想只使用?
这篇关于无法删除缓冲区。没有要删除的缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!