Home > Backend Development > PHP Tutorial > php生成excel文件打开报错?!!!

php生成excel文件打开报错?!!!

WBOY
Release: 2016-06-06 20:07:51
Original
1276 people have browsed it

<code>public function check_daochu($data) {
        ob_end_clean();   //***这里再加一个
        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/IOFactory');
        $objPHPExcel = new PHPExcel();
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        //设置表头
        $this->Excel_bt($objPHPExcel, $data['bt_data']);
        //导出的类容
        $this->Excel_lr($objPHPExcel, $data['data']);
        //设置单元格的宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('b')->setWidth(15);
        foreach ($data['hb_data'] as $key => $value) {
            //合并单元格
            $objPHPExcel->getActiveSheet()->mergeCells($key . ':' . $value);
        }
        //水平垂直居中设置
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $fieldname = iconv('utf-8', 'gb2312', date("Y-m-d", time()) . '_' . $data['bt_data']['A1']);
        ob_end_clean();

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename="' . $fieldname . '.xlsx"');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
    }
    
//设置导出标题的方法
    public function Excel_bt($objPHPExcel, $bt_data) {
        foreach ($bt_data as $key => $value) {
            $objPHPExcel->getActiveSheet()->setCellValue($key, $value);
        }
    }

    //设置导出的内容
    public function Excel_lr($objPHPExcel, $select_data) {
        $i = 3;
        $data = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        foreach ($select_data as $key => $value) {
            $j = 0;
            foreach ($value as $k => $v) {
                $objPHPExcel->getActiveSheet()->setCellValue($data[$j] . $i, $v);
                $j++;
            }
            $i++;
        }
    }</code>
Copy after login
Copy after login

生成EXCEL打开文件,报下图的错误?何解呢?(更让人费解的是,同样的代码块在另一个项目中完美运行!)
php生成excel文件打开报错?!!!

补充:自己找了问题,做了如下尝试,但是没一个可行的

  1. 我生成.txt文件,打开时发现乱码,于是我想到字符编码的问题,于是在check_daochu这个方法中,把data这个数组用iconv函数转换编码,不行!

  2. 在头文件里那里加入header("Content-Type:application/vnd.ms-execl;charset=utf-8");,不行!

  3. 把另外一个项目中相同的类库代码块都覆盖过来,不行!

打开.txt文件是这样子的

php生成excel文件打开报错?!!!

我擦、问题解决了!!!缓冲区的问题,看了这个文章才有一点明白,我明明在输出前加了ob_end_clean了,但是这还不够,还要在生成excel内容前加一个ob_end_clean,也就是在check_daochu方法的第一行。不知道为什么这里还要加一个,但是问题也已经解决了。

回复内容:

<code>public function check_daochu($data) {
        ob_end_clean();   //***这里再加一个
        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/IOFactory');
        $objPHPExcel = new PHPExcel();
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        //设置表头
        $this->Excel_bt($objPHPExcel, $data['bt_data']);
        //导出的类容
        $this->Excel_lr($objPHPExcel, $data['data']);
        //设置单元格的宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('b')->setWidth(15);
        foreach ($data['hb_data'] as $key => $value) {
            //合并单元格
            $objPHPExcel->getActiveSheet()->mergeCells($key . ':' . $value);
        }
        //水平垂直居中设置
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $fieldname = iconv('utf-8', 'gb2312', date("Y-m-d", time()) . '_' . $data['bt_data']['A1']);
        ob_end_clean();

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename="' . $fieldname . '.xlsx"');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
    }
    
//设置导出标题的方法
    public function Excel_bt($objPHPExcel, $bt_data) {
        foreach ($bt_data as $key => $value) {
            $objPHPExcel->getActiveSheet()->setCellValue($key, $value);
        }
    }

    //设置导出的内容
    public function Excel_lr($objPHPExcel, $select_data) {
        $i = 3;
        $data = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        foreach ($select_data as $key => $value) {
            $j = 0;
            foreach ($value as $k => $v) {
                $objPHPExcel->getActiveSheet()->setCellValue($data[$j] . $i, $v);
                $j++;
            }
            $i++;
        }
    }</code>
Copy after login
Copy after login

生成EXCEL打开文件,报下图的错误?何解呢?(更让人费解的是,同样的代码块在另一个项目中完美运行!)
php生成excel文件打开报错?!!!

补充:自己找了问题,做了如下尝试,但是没一个可行的

  1. 我生成.txt文件,打开时发现乱码,于是我想到字符编码的问题,于是在check_daochu这个方法中,把data这个数组用iconv函数转换编码,不行!

  2. 在头文件里那里加入header("Content-Type:application/vnd.ms-execl;charset=utf-8");,不行!

  3. 把另外一个项目中相同的类库代码块都覆盖过来,不行!

打开.txt文件是这样子的

php生成excel文件打开报错?!!!

我擦、问题解决了!!!缓冲区的问题,看了这个文章才有一点明白,我明明在输出前加了ob_end_clean了,但是这还不够,还要在生成excel内容前加一个ob_end_clean,也就是在check_daochu方法的第一行。不知道为什么这里还要加一个,但是问题也已经解决了。

猜测$objPHPExcel需要引用传参

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template