1

我有一些 Cyrilic 数据,我想用 Codeigniter 将其导出为 .csv 文件。问题是方法 csv_from_result() 弄乱了这些字符。

$this->load->dbutil();
$delimiter = ",";
$newline = "\r\n";

$report_result =  $this->data_model->get_data_result();
$data['csv']    = $this->dbutil->csv_from_result($report_result, $delimiter, $newline);

在我看来,我有:

<?php
header("Content-type: application/csv-tab-delimited-table; charset=utf-8" );
header("Content-Disposition: attachment; filename=csvdata.csv" );
header("Content-Transfer-Encoding: binary" );
  echo $csv; 
?> 

输出文件包含如下混乱的字符:

ТеÑÑ‚ пиÑмо","54","Референц
4

3 回答 3

2

来自 ci 的 csv_from_result 生成没有 BOM 的 utf-8,你可以在这里找到区别, 所以你现在需要做的就是将“utf-8 without BOM”转换为“utf-8”,你可以用下面的代码来做.

$this->load->dbutil();
$delimiter = ",";
$newline = "\r\n";

$report_result =  $this->data_model->get_data_result();
$CSV_data      = $this->dbutil->csv_from_result($report_result, $delimiter, $newline);
$CSV_data = chr(239) . chr(187) . chr(191) .$CSV_data;
$data['csv'] = $CSV_data;
于 2014-02-22T17:48:40.757 回答
1

我最终将PHPExcel 用于 Codeigniter。这就像一个魅力。

于 2013-07-30T09:20:58.430 回答
0

尝试这个:

$this->load->dbutil();
$query = $this->db->query($sql);
$delimiter = ",";
$newline = "\r\n";

header ("Content-disposition: attachment; filename=csvoutput_" . time() . ".csv") ;
echo mb_convert_encoding($this->dbutil->csv_from_result($query, $delimiter, $newline), "ISO-8859-1", "UTF-8");
于 2012-08-02T19:36:11.607 回答