2016-03-21 12 views
1

現在、私はエクスポートするためのクラスをExcelに用意していますが、他のエンコードでは英語には最適です。それは間違っている漢字は、関数の開始が、運たくさん助けるためPHPを使用してExcelをエクスポートするとエンコード文字列が正しくない

おかげで

header("Content-type: text/html; charset=utf-8"); 

を追加しようとしました

https://github.com/bcit-ci/CodeIgniter/wiki/Export-to-Excel-2013

<?php 
if (!defined('BASEPATH')) 
    exit('No direct script access allowed'); 

/* 
* Excel library for Code Igniter applications 
* Based on: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006 
* Tweaked by: Moving.Paper June 2013 
*/ 

class Export { 

    function to_excel($array, $filename) { 
     header('Content-type: application/vnd.ms-excel'); 
     header('Content-Disposition: attachment; filename=' . $filename . '.xls'); 

     //Filter all keys, they'll be table headers 
     $h = array(); 
     foreach ($array->result_array() as $row) { 
      foreach ($row as $key => $val) { 
       if (!in_array($key, $h)) { 
        $h[] = $key; 
       } 
      } 
     } 
     //echo the entire table headers 
     echo '<table><tr>'; 
     foreach ($h as $key) { 
      $key = ucwords($key); 
      echo '<th>' . $key . '</th>'; 
     } 
     echo '</tr>'; 

     foreach ($array->result_array() as $row) { 
      echo '<tr>'; 
      foreach ($row as $val) 
       $this->writeRow($val); 
     } 
     echo '</tr>'; 
     echo '</table>'; 
    } 

    function writeRow($val) { 
     echo '<td>' . utf8_decode($val) . '</td>'; 
    } 

} 

をコードしていました。繁体字中国語(Big5の)数

header("Content-type: text/html; charset=big5"); 

の詳細については

答えて

2

は、PHPExcelを使用し、完全に

$this->load->library('PHPExcel'); 
     $this->load->library('PHPExcel/IOFactory'); 

     $objPHPExcel = new PHPExcel(); 
     $objPHPExcel->getProperties()->setTitle("Trial Report"); 

     // Assign cell values 
     $objPHPExcel->setActiveSheetIndex(0); 

     //Filter all keys, they'll be table headers 
     $h = array(); 
     foreach ($result as $row) { 
      foreach ($row as $key => $val) { 
       if (!in_array($key, $h)) { 
        $h[] = $key; 
       } 
      } 
     } 

     //Writing the first header row 
     foreach ($h as $key => $val) { 
      $val = ucwords($val); 
      $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($key, 1, $val); //first row is 1 
     } 

     $pos = 0; 
     foreach ($result as $r_key => $row) { 
      foreach ($row as $col) { 
       $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($pos, ($r_key + 2), $col); //skip the first row 
       $pos++; 
      } 
      $pos = 0; 
     } 

     $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 

     header('Content-type: application/vnd.ms-excel'); 
     header('Content-Disposition: attachment; filename="export.xls"'); 
     $objWriter->save('php://output'); 
の作品
1

:代わりにカスタムクラスの

Chinese Simplified (EUC) 
    ##charset=EUC-CN 
Chinese Simplified (GB2312) 
    ##charset=gb2312 
Chinese Simplified (HZ) 
    ##charset=hz-gb-2312 
Chinese Simplified (Mac) 
    ##charset=x-mac-chinesesimp 
Chinese Traditional (Big5) 
    ##charset=big5 
Chinese Traditional (CNS) 
    ##charset=x-Chinese-CNS 
Chinese Traditional (Eten) 
    ##charset=x-Chinese-Eten 
Chinese Traditional (Mac) 
    ##charset=x-mac-chinesetrad 
    ##charset=950 

here is some fixing details and how to tut ...

+0

ありがとう助けるために。追加されたが、運がない、 "??"中国語の文字 – user782104

+0

兄弟の皆さん、私が追加したすべての中国語の文字セットを試してみてください。これらのうちいくつかの他のchinses langを持っていてもいいですか? –

+0

リンクから読んだもの、それはcharsetの使用以上のチュートリアルを持っています –

関連する問題