Is it possible to force Excel recognize UTF-8 CSV files automatically?

后端 未结 27 1654
醉梦人生
醉梦人生 2020-11-21 22:27

I\'m developing a part of an application that\'s responsible for exporting some data into CSV files. The application always uses UTF-8 because of its multilingual nature at

相关标签:
27条回答
  • 2020-11-21 23:10

    As I posted on http://thinkinginsoftware.blogspot.com/2017/12/correctly-generate-csv-that-excel-can.html:

    Tell the software developer in charge of generating the CSV to correct it. As a quick workaround you can use gsed to insert the UTF-8 BOM at the beginning of the string:

    gsed -i '1s/^\(\xef\xbb\xbf\)\?/\xef\xbb\xbf/' file.csv
    

    This command inserts the UTF-4 BOM if not present. Therefore it is an idempotent command. Now you should be able to double click the file and open it in Excel.

    0 讨论(0)
  • 2020-11-21 23:10

    A truly amazing list of answers, but since one pretty good one is still missing, I'll mention it here: open the csv file with google sheets and save it back to your local computer as an excel file.

    In contrast to Microsoft, Google has managed to support UTF-8 csv files so it just works to open the file there. And the export to excel format also just works. So even though this may not be the preferred solution for all, it is pretty fail safe and the number of clicks is not as high as it may sound, especially when you're already logged into google anyway.

    0 讨论(0)
  • 2020-11-21 23:10

    I am generating csv files from a simple C# application and had the same problem. My solution was to ensure the file is written with UTF8 encoding, like so:

    // Use UTF8 encoding so that Excel is ok with accents and such.
    using (StreamWriter writer = new StreamWriter(path, false, Encoding.UTF8))
    {
        SaveCSV(writer);
    }
    

    I originally had the following code, with which accents look fine in Notepad++ but were getting mangled in Excel:

    using (StreamWriter writer = new StreamWriter(path))
    {
        SaveCSV(writer);
    }
    

    Your mileage may vary - I'm using .NET 4 and Excel from Office 365.

    0 讨论(0)
提交回复
热议问题