欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-05-05 12:16:22
...

先贴代码:

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++;
        }
    }

导出出现如下的错误:

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

回复内容:

先贴代码:

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++;
        }
    }

导出出现如下的错误:

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

你的不行指的是什么?

register_shutdown_function(function(){
    $lastError = error_get_last();
    if ($lastError) {
        error_log(json_encode($lastError));
    }
});

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

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

相关标签: php