我正在尝试从下面的代码生成扩展名为 .xlsx 的 excel 文件。我可以很好地下载文件,但是当我用 Excel 表打开它时,我收到以下警告错误。Excel 无法打开文件“dindi.xlsx”,因为文件格式或文件扩展名无效。确保文件未损坏并且文件扩展名与文件格式匹配。当我用记事本打开它时,该文件出现以下错误:
注意:ob_end_clean() [ref.outcontrol]:删除缓冲区失败。没有要删除的缓冲区
下面是我正在尝试做的代码。
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);
}