E
E
Exelenz2014-05-08 14:24:21
PHP
Exelenz, 2014-05-08 14:24:21

Correct csv export to utf-8 with BOM

You need to export data from the database to open it in excel.
I create csv where separator is the \t sign.
The encoding in the base is utf-8, in php it is also utf-8, when exporting I use the following headers:

header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=".$dt.".xls");
header("Pragma: no-cache");

English letters are fine, Russian excel does not see. If you open the csv file locally and save with BOM, everything works.
If you add BOM before displaying content - excel understands Russian letters, but does not see the separator "\t" - i.e. is not split by columns, all columns are merged in each row.
Tried other separators - excel does not understand them by default.
How to be? how to send csv so that excel opens correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
glukkkk, 2014-05-08
@glukkkk

I have everything correctly Excel determines. I use a semicolon separator and insert BOM before writing to .csv:

$csv = fopen('file.csv', 'w');
fprintf($csv, chr(0xEF).chr(0xBB).chr(0xBF));
...

V
Vit, 2014-05-08
@fornit1917

When opening (importing) a CSV file in Excel, you can specify a bunch of options, incl. and delimited encoding and even cell formatting method.
In LibreOffice, by default, it asks for these options; in MS, you need to call the import wizard from text files.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question