使用utf-8 unicode导出php中的excel文件

I want to export mysql with Php (CodeIgniter) in excel format, but I have problem with UFT-8 characters. it shows the UTF-8 characters ugly.

It is my code

class Excel extends CI_controller{

function index(){

    $this->load->model('user_model');
    $data = $this->user_model->profile_detail_excel();  

      function cleanData(&$str)
      {
        $str = preg_replace("/\t/", "\\t", $str);
        $str = preg_replace("/?
/", "\
", $str);
        if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
        $str = chr(255).chr(254).mb_convert_encoding( $str, 'UTF-16LE', 'UTF-8');           
        }

      // filename for download
      $filename = "website_data_" . date('Ymd') . ".xls";

    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Type: application/vnd.ms-excel;  charset=UTF-16LE");


      $flag = false;
      foreach($data as $row) {
        if(!$flag) {
          // display field/column names as first row
          echo implode("\t", array_keys($row)) . "
";
          $flag = true;
        }
        array_walk($row, 'cleanData');
        echo implode("\t", array_values($row)) . "
";
      }
      exit;


}

I appreciate any help

查看全部
dongpixi2648
dongpixi2648
2015/10/27 10:04
  • excel
  • codeigniter
  • php
  • export
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复