Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate CSV via Apache CSV in UTF-8

how to write CSV File in UTF-8 via Apache CSV?

I am trying generate csv by following code where Files.newBufferedWriter() encode text into UTF-8 by default, but when I open generated text in excel there are senseless characters.

I create CSVPrinter like this:

CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(Paths.get(filePath)), CSVFormat.EXCEL);

next I set headers

csvPrinter.printRecord(headers);

and next in loop I print values into writer like this

csvPrinter.printRecord("value1", "valu2", ...);

I also tried upload file into online CSV lint validator and it tells that I am using ASCII-8BIT instead of UTF-8. What I did wrong?

like image 334
Denis Stephanov Avatar asked Dec 14 '22 11:12

Denis Stephanov


1 Answers

Microsoft software tends to assume windows-12* or UTF-16LE charsets, unless the content starts with a byte order mark which the software will use to identify the charset. Try adding a byte order mark at the start of your file:

try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(filePath))) {

    writer.write('\ufeff');

    CSVPrinter csvPrinter = new CSVPrinter(writer);

    //...
}
like image 158
VGR Avatar answered Jan 30 '23 02:01

VGR