【发布时间】:2015-09-10 06:48:54
【问题描述】:
在将数据从数据库导出到 csv 时需要它的一点帮助。 不幸的是,我的国家有特殊字符 čćšžđ。 导出工作正常,如果在 Notepad++ 中打开 .csv,它会给我正确的单词形式。但是当它在 Excel 中打开时,特殊字符就像象形文字。 数据库中的示例我有: 导出前数组中的 IZLETIŠTE STOJČIĆ:IZLETIŠTE STOJČIĆ,在 Notepad++ 中: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);
}
【问题讨论】: