Heim > Backend-Entwicklung > PHP-Tutorial > PHP导出Excel文件问题!求解救~~

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

WBOY
Freigeben: 2016-06-06 20:07:56
Original
934 Leute haben es durchsucht

先贴代码:

<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>
Nach dem Login kopieren
Nach dem Login kopieren

导出出现如下的错误:

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>
Nach dem Login kopieren
Nach dem Login kopieren

导出出现如下的错误:

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

你的不行指的是什么?

<code>register_shutdown_function(function(){
    $lastError = error_get_last();
    if ($lastError) {
        error_log(json_encode($lastError));
    }
});</code>
Nach dem Login kopieren

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

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

Verwandte Etiketten:
php
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage