简体   繁体   中英

Java - escape line breaks in CSV for Excel

I generate csv file in Java code. I use org.apache.commons.lang3.StringEscapeUtils to escape values for csv. Some values contain line breaks. When I open the resulted file in Open Office everything is OK. However when I open it in Microsoft Excel 10 the table is garbled - the text on new line is moved to another cell breaking the whole structure. Is there a way to escape line breaks in Java, so that csv will open properly in Excel?

Use a CSV library to handle this for you. Example with univocity-parsers

List<Object[]> allRows = getRowsToWriteFromSomewhere();
new CsvWriter(Csv.writeExcel()).writeRowsAndClose(allRows);

Hope this helps.

Disclaimer: I'm the author of this libary. It's open-source and free (Apache 2.0 license)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM