zoukankan      html  css  js  c++  java
  • 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

    本文来自博客园,作者:袁与张,转载请注明原文链接:https://www.cnblogs.com/ytyzhn/p/13208187.html

  • 相关阅读:
    【BZOJ 4581】【Usaco2016 Open】Field Reduction
    【BZOJ 4582】【Usaco2016 Open】Diamond Collector
    【BZOJ 4580】【Usaco2016 Open】248
    【BZOJ 3754】Tree之最小方差树
    【51Nod 1501】【算法马拉松 19D】石头剪刀布威力加强版
    【51Nod 1622】【算法马拉松 19C】集合对
    【51Nod 1616】【算法马拉松 19B】最小集合
    【51Nod 1674】【算法马拉松 19A】区间的价值 V2
    【BZOJ 2541】【Vijos 1366】【CTSC 2000】冰原探险
    【BZOJ 1065】【Vijos 1826】【NOI 2008】奥运物流
  • 原文地址:https://www.cnblogs.com/ytyzhn/p/13208187.html
Copyright © 2011-2022 走看看