【问题标题】:When exporting to csv special characters (čćšžđ) don't work导出为 csv 特殊字符 (čćšžđ) 时不起作用
【发布时间】: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);
}

【问题讨论】:

    标签: php excel csv


    【解决方案1】:

    试试

    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

    【讨论】:

      【解决方案2】:

      您可以在返回之前尝试在$output 上进行搜索和替换。这些字符可能是百分比编码的,所以你可以试试这个:

      $search = array('%C4%8D','%C4%87','%C5%A1','%C5%BE','%C4%91');
      $replace = array('č','ć','š','ž','đ');
      $output = str_replace($search, $replace, $output);
      

      如果可行,请告诉我。

      【讨论】:

        猜你喜欢
        • 2017-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-02
        • 1970-01-01
        相关资源
        最近更新 更多