php - 未能删除缓冲区。没有要删除的缓冲区

标签 php codeigniter phpexcel

我正在尝试从下面的代码生成一个扩展名为 .xlsx 的 excel 文件。我可以很好地下载文件,但是当我用 excel 工作表打开它时,我收到以下警告错误。Excel 无法打开文件“dindi.xlsx”,因为文件格式或文件扩展名无效。确保文件未损坏并且文件扩展名与文件格式匹配。 当我用记事本打开它时,文件有以下错误:

Notice: ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete

下面是我正在尝试做的代码。

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);



    }

最佳答案

该错误只是告诉您没有要删除的缓冲区。 要避免它,只需使用:

if (ob_get_contents()) ob_end_clean();

(检查是否有事件的输出缓冲区) 或者:

if (ob_get_length()) ob_end_clean();

(检查缓冲区中是否有非空字符串) 正如@Venu 所建议的。

您还在那里调用了两次 ob_end_clean();。这只适用于可堆叠的缓冲区。来自 PHP 手册:

This function discards the contents of the topmost output buffer and turns off this output buffering.

你确定你不想只使用 ob_clean()

关于php - 未能删除缓冲区。没有要删除的缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14549110/

相关文章:

php - 使用php从数据库检索数据到android

php - DOM PDF 在 Mac 上呈现,但在 PC 上不呈现

php - 如何在PhpSpreadsheet中对齐单元格值?

PHPExcel 数据系列不工作

php - 为 PHPexcel 正确编码 CSV 文件

php - 更新 Doctrine 中的总关系计数器

php - 套接字、PHP、本地端口

php - 使用 smarty php 打印链接

php - 使用 cron 作业发送带有不同附件的群发电子邮件

php - 我应该如何处理我的 Codeigniter 模型中的错误?