我有一些数据在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","Референц
答案 0 :(得分:2)
csv_from_result生成没有BOM的utf-8,您可以找到差异here 所以你现在需要做的就是将“utf-8无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;
答案 1 :(得分:1)
我最终使用PHPExcel for Codeigniter。这就像一个魅力。
答案 2 :(得分: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");