Home > Software design >  Log4j2 write to CSV for Excel without garbled characters
Log4j2 write to CSV for Excel without garbled characters

Time:04-21

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!

CodePudding user response:

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="&#xFEFF;id,question,answer\n"
                    charset="UTF-8" />
  • Related