首頁 >後端開發 >php教程 >PHP导出Excel文件问题!求解救~~

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

WBOY
WBOY原創
2016-06-06 20:07:56929瀏覽

先贴代码:

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

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

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn