dongpixi2648
2015-10-27 10:04
浏览 70
已采纳

使用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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douluokuang7184 2015-11-07 09:16
    已采纳

    I use PHPExcel plugin for codeigniter. it works perfect for utf-8

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duansaoguan7955 2015-10-27 21:00

    I'm not sure this will answer the UTF-8 problem. But here is an elegant way to create CSV files with CodeIgniter - use $this->dbutil->csv_from_result($db_result); Documentation Here

    Model:

    class User_model extends CI_Model
    {
      function __construct()
      {
        parent :: __construct();
        $this->load->database();
        $this->load->dbutil();
      }
    
      function profile_detail_excel($condition)
      {
        $query = $this->db->get_where('profile_table', $condition);
        if($query->num_rows() === 0)
        {
          return NULL;
        }
        //Here's the meat
        $csv = $this->dbutil->csv_from_result($query);
        // csv_from_result accepts additional params allowing you
        // to set the delimiter, newline and enclosure characters
    
        //if you want to try conveting the encoding do it now
        //$csv = mb_convert_encoding( $csv, 'UTF-16LE', 'UTF-8'); 
    
        return $csv;
      }
    
    }
    

    Note it returns a CSV string of the whole profile.

    Controller:

    class Xl_xport extends CI_Controller
    {
      function __construct()
      {
        parent::__construct();
        $this->load->model('user_model');
      }
    
      function index()
      {
        $csv = $this->user_model->profile_detail_excel();
        if(!empty($csv))
        {
          // Strongly recommend using file type .csv because
          // certain versions of Excel complain about .xls that isn't really .xls
          $filename = "website_data_".date('Ymd').".csv";
          header("Content-Disposition: attachment; filename=\"$filename\"");
          header("Content-Type: text/csv");
    
          echo $csv;
        }
        else
        {
          //you may want to handle this some other way
          throw new Exception("No Profile Found");
        }
      }
    
    评论
    解决 无用
    打赏 举报