Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write UTF-8 BOM with supercsv

I am using supercscv to write an utf-8 encoded csv. It produces a normal file but excel doesn't recognize it as utf-8 cause it's dumb, excel lost without the bom marker so any special characters are corrupted when opened with excel.

Is there a way to write a file as UTF-8 with BOM with supercsv ? I can't find it.

Thanks

like image 677
allaf Avatar asked Aug 18 '15 12:08

allaf


2 Answers

As supercsv probably wraps a Writer:

Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
writer.write('\uFEFF'); // BOM for UTF-*
... new BeanWriter(writer, CsvPreference.STANDARD_PREFERENCE);
like image 54
Joop Eggen Avatar answered Nov 14 '22 19:11

Joop Eggen


In my experience MS Excel always opens csv files in the default MS Office charset. In my case, it was always Windows 1252 (Spain), even in not Windows Machines (MS Office for OSX). The only way to deal with it was to write CSV files with this charset.

byte[] csvFileBytes = dataObject.toCSVString().getBytes(Charset.forName("Windows-1252"));

MS Excel seems to never use another charset to open CSV files. You can check this post: Is it possible to force Excel recognize UTF-8 CSV files automatically?

like image 20
Ricardo Vila Avatar answered Nov 14 '22 19:11

Ricardo Vila