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

后端 未结 27 1655
醉梦人生
醉梦人生 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 22:51

    Simple vba macro for opening utf-8 text and csv files

    Sub OpenTextFile()
    
       filetoopen = Application.GetOpenFilename("Text Files (*.txt;*.csv), *.txt;*.csv")
       If filetoopen = Null Or filetoopen = Empty Then Exit Sub
    
       Workbooks.OpenText Filename:=filetoopen, _
       Origin:=65001, DataType:=xlDelimited, Comma:=True
    
    End Sub
    

    Origin:=65001 is UTF-8. Comma:True for .csv files distributed in colums

    Save it in Personal.xlsb to have it always available. Personalise excel toolbar adding a macro call button and open files from there. You can add more formating to the macro, like column autofit , alignment,etc.

    0 讨论(0)
  • 2020-11-21 22:52

    We have used this workaround:

    1. Convert CSV to UTF-16 LE
    2. Insert BOM at beginning of file
    3. Use tab as field separator
    0 讨论(0)
  • 2020-11-21 22:55

    Had the same problems with PHP-generated CSV files. Excel ignored the BOM when the Separator was defined via "sep=,\n" at the beginning of the content (but of course after the BOM).

    So adding a BOM ("\xEF\xBB\xBF") at the beginning of the content and setting the semicolon as separator via fputcsv($fh, $data_array, ";"); does the trick.

    0 讨论(0)
  • 2020-11-21 22:55

    Old question but heck, the simplest solution is:

    1. Open CSV in Notepad
    2. Save As -> select the right encoding
    3. Open the new file
    0 讨论(0)
  • 2020-11-21 22:55

    I faced the same problem a few days ago, and could not find any solution because I cannot use the import from csv feature because it makes everything to be styled as string.

    My solution was to first open the file with notpad++ and change the encode to ASCII. Then just opened the file in excel and it worked as expected.

    0 讨论(0)
  • 2020-11-21 22:55

    In php you just prepend $bom to your $csv_string:

    $bom = sprintf( "%c%c%c", 239, 187, 191); // EF BB BF
    file_put_contents( $file_name, $bom . $csv_string );
    

    Tested with MS Excel 2016, php 7.2.4

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