Loading

tinkphp导出execl乱码问题

最近项目中测试execl导出出现了乱码问题,查阅资料整理如下:

execl导出代码片段:

    public function exportExcel($expTitle,$expCellName,$expTableData){
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = 告警记录_.date('Ymd');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new PHPExcel();
        $cellName = 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','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'); 
        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格 
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]); 
        } 
        for($i=0;$i<$dataNum;$i++){
          for($j=0;$j<$cellNum;$j++){
            $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
          }             
        }  
        
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
        $objWriter->save('php://output'); 
        exit;   
    }
   function execlall(){//全部导出Excel
        parent::YanZheng('loginemails','__APP__/Index/renew');  //验证非法用户进出页面
        $xlsName  = "告警记录";
        $xlsCell  = array(
        array('Gaojing_jifang','所属机房'),
        array('Gaojing_panduan','状态'),
        array('Gaojing_equipment','告警设备'),
        array('Gaojing_hpdate','告警发生时间'), 
        array('Gaojing_log','告警日志'),
        array('Gaojing_fenxi','告警分析'),
        array('Gaojing_person','记录人'),
        array('Gaojing_date','记录时间'),
        );
        $Person=new PersonModel();
        $condition1[$Person->_id]=session('loginemails');            //判定条件
        $list1=$Person->where($condition1)->find();
        $condition["Gaojing_jifang"]=$list1["Person_bumen"];
        $xlsModel = new GaojingModel();
        $condition["Gaojing_panduan"]=$_GET['daochu'];
        
        ob_clean();   //清除缓存
if($_GET['daochu']=="all"){ $tiaojian["Gaojing_jifang"]=$list1["Person_bumen"]; $xlsData = $xlsModel->order('Gaojing_date desc')->where($tiaojian)->Field('Gaojing_jifang,Gaojing_panduan,Gaojing_equipment,Gaojing_hpdate,Gaojing_log,Gaojing_fenxi,Gaojing_person,Gaojing_date')->select(); $this->exportExcel($xlsName,$xlsCell,$xlsData); } $xlsData = $xlsModel->order('Gaojing_date desc')->where($condition)->Field('Gaojing_jifang,Gaojing_panduan,Gaojing_equipment,Gaojing_hpdate,Gaojing_log,Gaojing_fenxi,Gaojing_person,Gaojing_date')->select(); $this->exportExcel($xlsName,$xlsCell,$xlsData); }
只需要在execlall函数中加入ob_clean()问题得以解决,当然方法不止一个;
关于ob_clean可查阅资料 https://coderschool.cn/1963.html
posted @ 2020-06-29 15:27  袁与张  阅读(175)  评论(1编辑  收藏  举报