<dl id="11fd5"></dl>
<noframes id="11fd5"><video id="11fd5"></video>
<dl id="11fd5"><i id="11fd5"><delect id="11fd5"></delect></i></dl>
<dl id="11fd5"></dl>
<dl id="11fd5"><i id="11fd5"></i></dl><video id="11fd5"><i id="11fd5"><delect id="11fd5"></delect></i></video><video id="11fd5"></video>
<dl id="11fd5"></dl>
<video id="11fd5"></video>
<video id="11fd5"><dl id="11fd5"><delect id="11fd5"></delect></dl></video><dl id="11fd5"></dl> <i id="11fd5"></i><dl id="11fd5"><i id="11fd5"></i></dl>
<dl id="11fd5"><delect id="11fd5"></delect></dl>
<dl id="11fd5"></dl>
<i id="11fd5"><i id="11fd5"></i></i>
<dl id="11fd5"><i id="11fd5"></i></dl>
0712-2888027 189-8648-0214
微信公眾號

孝感風信網絡科技有限公司微信公眾號

當前位置:主頁 > 技術支持 > DedeCMS > dedecms后臺增加php導出到excel功能不出現亂碼

dedecms后臺增加php導出到excel功能不出現亂碼

時間:2023-09-22來源:風信官網 點擊: 472次

dede織夢系統怎樣導出后臺的文章或自定義模型中的數據到excel,并且不出現亂碼

方法/步驟

  1. 在后臺目錄創建一個php文件toexcel.php,在最上面加入代碼;

    require_once(dirname(__FILE__).'/config.php');

    require_once(DEDEINC.'/typelink.class.php');

    require_once(DEDEINC.'/datalistcp.class.php');

    require_once(DEDEADMIN.'/inc/inc_list_functions.php');

    dede織夢后臺增加php導出到excel功能不出現亂碼
  2. 加入導出到excel類;

    class Excel

    {    

      private $head;     

    private $body; 

      public function addHeader($arr){        

      foreach($arr as $headVal){            

    $headVal = $this->charset($headVal);             

    $this->head .= "{$headVal}\t ";        

    }         

    $this->head .= "\n";     

      public function addBody($arr){         

      foreach($arr as $arrBody){             

    foreach($arrBody as $bodyVal){                 

    $bodyVal = $this->charset($bodyVal);                 

    $this->body .= "{$bodyVal}\t ";             

    }             

    $this->body .= "\n";        

    }     

      public function downLoad($filename=''){         

      if(!$filename)             

      $filename = date('YmdHis',time()).'.xls';         

      header("Content-type:application/vnd.ms-excel");         

      header("Content-Disposition:attachment;filename=$filename");          

      header("Content-Type:charset=gb2312");         

      if($this->head)            

      echo $this->head;         

        echo $this->body;     

       public function charset($string){         

    return mb_convert_encoding($string,'GBK','auto');    

       } 

    dede織夢后臺增加php導出到excel功能不出現亂碼
  3. 代碼解釋:

    1.輸出列名數組,并轉碼

    public function addHeader($arr){        

      foreach($arr as $headVal){            

    $headVal = $this->charset($headVal);             

    $this->head .= "{$headVal}\t ";        

    }         

    $this->head .= "\n";     

    dede織夢后臺增加php導出到excel功能不出現亂碼
  4. 2.輸出導出內容數組,并轉碼

    public function addBody($arr){         

      foreach($arr as $arrBody){             

    foreach($arrBody as $bodyVal){                 

    $bodyVal = $this->charset($bodyVal);                 

    $this->body .= "{$bodyVal}\t ";             

    }             

    $this->body .= "\n";        

    }     

    dede織夢后臺增加php導出到excel功能不出現亂碼
  5. 3.設置header頭部信息和導出到excel內容,并輸出到瀏覽器

    public function downLoad($filename=''){         

      if(!$filename)             

      $filename = date('YmdHis',time()).'.xls';         

      header("Content-type:application/vnd.ms-excel");         

      header("Content-Disposition:attachment;filename=$filename");          

      header("Content-Type:charset=gb2312");         

      if($this->head)            

      echo $this->head;         

        echo $this->body;     

    dede織夢后臺增加php導出到excel功能不出現亂碼
  6. 4.轉碼,這里不用iconv函數,有可能會與gd沖突導致輸出空白。用

     public function charset($string){         

    return mb_convert_encoding($string,'GBK','auto');    

       } 

    dede織夢后臺增加php導出到excel功能不出現亂碼
  7. 7.調用方法;

    $excel = new Excel();  

    $excel->addHeader(array('列一','列二','列三','列四')); 

    global $dsql; 

    $sql="select 列一字段,列二字段,列三字段,列四字段 from 表名"; 

    $dsql->SetQuery($sql);

    $dsql->Execute();

    while($row = $dsql->GetArray()){

    $list[]=$row;

    }

    unset($row);

    $excel->addBody($list);  

    $excel->downLoad(); 

    dede織夢后臺增加php導出到excel功能不出現亂碼
  8. 后天添加導出到excel代碼:

    找到后臺目錄下的templets目錄,下面有個content_list.htm文件,

    找到<a href="javascript:;" onClick="cAtts('attsDel',event,this)" class="coolbg">&nbsp;刪除屬性&nbsp;</a>

    在后面加一段代碼

    <?php if($channelid==1) echo " <a href=\"toexcel.php\" class=\"coolbg\" target=\"_blank\">導出到excel</a>\r\n"; ?>

    $channelid就是你的模型id,根據你導出的表填寫。填寫完之后打開后臺欄目列表就出現導出按鈕

    dede織夢后臺增加php導出到excel功能不出現亂碼
    END

注意事項

  • 轉碼問題,根據自己的實際情況
  • 導出字段,多表或自定義模型的表可以通過left join
欄目列表
推薦內容
熱點內容
展開
亚洲欧美日韩在线观看播放