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

后端 未结 27 1648
醉梦人生
醉梦人生 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:09

    It is incredible that there are so many answers but none answers the question:

    "When I was asking this question, I asked for a way of opening a UTF-8 CSV file in Excel without any problems for a user,..."

    The answer marked as the accepted answer with 200+ up-votes is useless for me because I don't want to give my users a manual how to configure Excel. Apart from that: this manual will apply to one Excel version but other Excel versions have different menus and configuration dialogs. You would need a manual for each Excel version.

    So the question is how to make Excel show UTF8 data with a simple double click?

    Well at least in Excel 2007 this is not possible if you use CSV files because the UTF8 BOM is ignored and you will see only garbage. This is already part of the question of Lyubomyr Shaydariv:

    "I also tried specifying UTF-8 BOM EF BB BF, but Excel ignores that."

    I make the same experience: Writing russian or greek data into a UTF8 CSV file with BOM results in garbage in Excel:

    Content of UTF8 CSV file:

    Colum1;Column2
    Val1;Val2
    Авиабилет;Tλληνικ
    

    Result in Excel 2007:

    A solution is to not use CSV at all. This format is implemented so stupidly by Microsoft that it depends on the region settings in control panel if comma or semicolon is used as separator. So the same CSV file may open correctly on one computer but on anther computer not. "CSV" means "Comma Separated Values" but for example on a german Windows by default semicolon must be used as separator while comma does not work. (Here it should be named SSV = Semicolon Separated Values) CSV files cannot be interchanged between different language versions of Windows. This is an additional problem to the UTF-8 problem.

    Excel exists since decades. It is a shame that Microsoft was not able to implement such a basic thing as CSV import in all these years.


    However, if you put the same values into a HTML file and save that file as UTF8 file with BOM with the file extension XLS you will get the correct result.

    Content of UTF8 XLS file:

    Colum1Column2
    Val1Val2
    АвиабилетTλληνικ

    Result in Excel 2007:

    You can even use colors in HTML which Excel will show correctly.

    
    
    Colum1Column2
    Val1Val2
    АвиабилетTλληνικ

    Result in Excel 2007:

    In this case only the table itself has a black border and lines. If you want ALL cells to display gridlines this is also possible in HTML:

    
        
            
            
                
                    
                        
                            MySuperSheet
                            
                                
                            
                        
                    
                
            
        
        
            
    Colum1Column2
    Val1Val2
    АвиабилетTλληνικ

    This code even allows to specify the name of the worksheet (here "MySuperSheet")

    Result in Excel 2007:

提交回复
热议问题