|
本帖最后由 qichengzx 于 2014-8-15 14:55 编辑
正好最近项目要用到导出csv,在论坛搜索也没找到乱码的解决办法,看到的几个帖子说用iconv('GB2312', 'UTF-8', $data);转也没成功。
今天试了下用mb_convert_encoding( $data,"GB2312", "UTF-8");成功导出并且在excel打开也不乱码了。Linux没装软件只能表示暂时不清楚
model代码:
PHP复制代码
public function export(){
$query = $this->db->query("SELECT * FROM xxx where id = 1");
$data = $this->dbutil->csv_from_result($query);
return $data;
}
复制代码
C 代码
PHP复制代码
public function export (){
$this->load->dbutil();
$this->load->helper('file');
$data = $this -> Ajax_model -> export();
$delimiter = ",";
$newline = "\r\n";
@ob_end_clean();
//print_r($data);
$data1 = mb_convert_encoding( $data,"GB2312", "UTF-8");
//print_r($data1);
if ( ! write_file ('./test.csv', $data1)){
echo 'Unable to write the file';
}else{
echo 'File written!';
}
}
复制代码
|
|