Php 导出为csv特殊字符(č;ć;š;ž;đ;)时,不要';行不通
在将数据从数据库导出到csv时几乎不需要它。 不幸的是,我国具有特殊的特点。 导出工作正常,如果在Notepad++中打开.csv,它会给出单词的正确形式。但在Excel中打开时,特殊字符是象形文字。 我拥有的数据库中的示例: 导出前数组中的IZLETIŠTE STOJĆIĆ:IZLETIŠTE STOJĆIĆ,在记事本++:IZLETIŠTE STOJĆIĆ中,但在Excel中我得到IZLETIĹTE STOJÄIƆ 为什么它不起作用? 这是代码,我需要添加一些东西还是需要在Excel中更改一些东西Php 导出为csv特殊字符(č;ć;š;ž;đ;)时,不要';行不通,php,excel,csv,Php,Excel,Csv,在将数据从数据库导出到csv时几乎不需要它。 不幸的是,我国具有特殊的特点。 导出工作正常,如果在Notepad++中打开.csv,它会给出单词的正确形式。但在Excel中打开时,特殊字符是象形文字。 我拥有的数据库中的示例: 导出前数组中的IZLETIŠTE STOJĆIĆ:IZLETIŠTE STOJĆIĆ,在记事本++:IZLETIŠTE STOJĆIĆ中,但在Excel中我得到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);
}
在返回之前,您可以尝试在
$output
上进行搜索和替换。字符可能是百分比编码的,因此您可以尝试以下方法:
$search = array('%C4%8D','%C4%87','%C5%A1','%C5%BE','%C4%91');
$replace = array('č','ć','š','ž','đ');
$output = str_replace($search, $replace, $output);
请让我知道这是否有效。试试看
protected function convertChar($text)
{
return @iconv('UTF-8','ISO-8859-2//TRANSLIT',$text);
}
iconv('UTF-8','您的_代码//translatit',$text)