PHP导出Excel文件问题!求解救~~

WBOY
发布: 2016-06-06 20:07:56
原创
888 人浏览过

先贴代码:

<code>public function check_daochu($data) {
        $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>
登录后复制
登录后复制

导出出现如下的错误:

PHP导出Excel文件问题!求解救~~

回复内容:

先贴代码:

<code>public function check_daochu($data) {
        $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>
登录后复制
登录后复制

导出出现如下的错误:

PHP导出Excel文件问题!求解救~~

你的不行指的是什么?

<code>register_shutdown_function(function(){
    $lastError = error_get_last();
    if ($lastError) {
        error_log(json_encode($lastError));
    }
});</code>
登录后复制

你用记事本打开看下有没有报错
一般这种情况是出错了

以前的帖子,还是没找到原因呢

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板