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
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.
We have used this workaround:
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.
Old question but heck, the simplest solution is:
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.
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