'Log4j2 write to CSV for Excel without garbled characters
I am using Log4j2 to write a output.csv file with UTF-8 charset.
After the output is complete, everything works well. But when I open the output.csv by Excel-2016, every Chinese characters become garbled.
I have noticed that Excel opens csv with ANSI encode by default, so many UTF-8 characters cannot display correctly.
One of the solution is adding byte order mark (BOM) at beginning of my output.csv by myself.
See: java-how-to-add-and-remove-bom-from-utf-8-file
But I hope log4j2 can do that for me. Is there any better solution/workaround ?
Here is my log4j2.xml
<RollingFile
name="Chat-Appender"
fileName="${logSavePath}/output.csv"
filePattern="${logArchivePath}/output_%d{yyyy-MM-dd}.csv">
<CsvParameterLayout
delimiter=","
format="Excel"
header="id,question,answer\n"
charset="UTF-8" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
</RollingFile>
Thanks for your help and suggestion!
Solution 1:[1]
If you want to have a BOM at the beginning of the file, you just need to insert it at the beginning of the header parameter:
<CsvParameterLayout delimiter=","
format="Excel"
header="id,question,answer\n"
charset="UTF-8" />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Piotr P. Karwasz |
