2015-11-08 09:28
浏览 32

如何在php [codeigniter]中将utf-8设置为csv文件

public function ExportCSV(){
    $delimiter = ",";
    $newline = "
    $slash = "∕";
    $filename = "testnaja.csv";
    $query = "select * from student where room_id = 011";
    $result = $this->db->query($query);
    $data = $this->dbutil->csv_from_result($result, $delimiter, $newline);               
    force_download($filename, $data);

I want to generate excel file exporting data from my table to it.
The output in .xls and .csv

In csv output is:

enter image description here

Please help. Thanks.

图片转代码服务由CSDN问答提供 功能建议

  public function ExportCSV(){
 $ this-> load-> dbutil();  
 $ this-> load-> helper('file'); 
 $ this-> load-> helper('download');  
 $ delimiter =“,”; 
 $ newline =“
 $ slash =“/”; 
 $ filename =“testnaja.csv”; 
 $ query =“select * from 学生where room_id = 011“; 
 $ result = $ this-> db-> query($ query); 
 $ data = $ this-> dbutil-> csv_from_result($ result,$ delimiter,$ 新队);  
 force_download($ filename,$ data); 



请帮忙。 谢谢。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • drnrxv9383 2015-11-08 14:50

    Change your force_download from

    force_download($filename, $data);

    To this:

    force_download($filename, "\xEF\xBB\xBF" . $data);

    which makes the output UTF-8 + BOM so it's recognized by MS Excel

    打赏 评论
  • dongsong1911 2015-11-08 11:20

    To setup codeigniter to download CSV format, use the following code.

    function _push_file($path, $name)
      // make sure it's a file before doing anything!
        // required for IE
        if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
        // get the file mime type using the file extension
        $mime = get_mime_by_extension($path);
        // Build the headers to push out the file properly.
        header('Pragma: public');     // required
        header('Expires: 0');         // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
        header('Cache-Control: private',false);
        header('Content-Type: '.$mime);  // Add the mime type from Code igniter.
        header('Content-Disposition: attachment; filename="'.basename($name).'"');  // Add the file name
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($path)); // provide file size
        header('Connection: close');
        readfile($path); // push it out

    Duplicate from : Codeigniter Force download files

    and also from : Download csv from codeigniter mysql

    打赏 评论
  • dongzhuang6177 2015-11-08 11:31

    I don't know if I understood you correctly, but if your problem is that you'd like Excel to recognize your CSV file as being UTF-8 encoded, you'll have to add a UTF-8 BOM to your file. Just prepend your CSV output with

    chr(239) . chr(187) . chr(191) // xEF xBB xBF

    For example:

    $data = chr(239) . chr(187) . chr(191) . $this->dbutil->csv_from_result($result, $delimiter, $newline);

    If your problem is about forcing a download, perhaps is going to help.

    打赏 评论

相关推荐 更多相似问题