从数据库导出到csv时需要很少的帮助。不幸的是,我国有特殊字符čćšžđ。导出工作正常,如果在记事本++中打开.csv,它会为我提供单词的正确形式。但是当它在Excel中打开时,特殊字符是象形文字。数据库中的示例我有:IZLETIŠTE STOJČIĆ in the array before export: IZLETIŠTE STOJČIĆ, in Notepad++ : IZLETIŠTE STOJČIĆ, but in Excel i get IZLETIĹ TE STOJÄŚIÄ
†为什么它不起作用?这是代码,我是否必须向其添加某些内容,或者是否需要在Excel中更改某些内容
function convertToCSV($data, $options) {
$exportName = implode($options['exportName']);
// setting the csv header
if (is_array($options) && isset($options['headers']) && is_array($options['headers'])) {
$headers = $options['headers'];
} else {
$headers = array(
'Content-Type' => 'text/csv,charset=UTF-8',
'Content-Disposition' => 'attachment; filename="'.$exportName.'.xls"'
);
}
$output = '';
// setting the first row of the csv if provided in options array
if (isset($options['firstRow']) && is_array($options['firstRow'])) {
$output .= implode(' ', $options['firstRow']);
$output .= "n"; // new line after the first line
}
// setting the columns for the csv. if columns provided, then fetching the or else object keys
if (isset($options['columns']) && is_array($options['columns'])) {
$columns = $options['columns'];
} else {
$objectKeys = get_object_vars($data[0]);
$columns = array_keys($objectKeys);
}
// populating the main output string
foreach ($data as $row) {
foreach ($columns as $column) {
$output .= str_replace(' ', ';', $row->$column);
$output .= ' ';
}
$output .= "n";
}
// calling the Response class make function inside my class to send the response.
// if our class is not a controller, this is required.
return Response::make($output, 200, $headers);
}
试试
protected function convertChar($text)
{
return @iconv('UTF-8','ISO-8859-2//TRANSLIT',$text);
}
iconv('UTF-8','your_code//TRANSLIT',$text(;
http://php.net/manual/en/function.iconv.php
您可以尝试在返回之前立即$output
进行搜索和替换。这些字符可能是百分比编码的,因此您可以尝试以下操作:
$search = array('%C4%8D','%C4%87','%C5%A1','%C5%BE','%C4%91');
$replace = array('č','ć','š','ž','đ');
$output = str_replace($search, $replace, $output);
如果有效,请告诉我。