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 @   袁与张  阅读(177)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示
主题色彩