It looks like you are printing all the records on the same line .

Other methods like printRecords will be more helpful :

String outputFile = savePath+".csv";
CSVPrinter csvFilePrinter = null;
CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader();
FileWriter fileWriter = new FileWriter(outputFile);
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

csvFilePrinter.printRecords(excelParser.getRecords());


fileWriter.flush();
fileWriter.close();
csvFilePrinter.close();
Answer from Arnaud on Stack Overflow
🌐
Medium
medium.com › @kaveeshapiumini1999 › data-export-using-apache-commons-csv-09a154243d97
Data Export using Apache Commons CSV | by Kaveeshapiumini | Medium
January 4, 2024 - Here, the List<Person> obtained from the JSON data is passed to the exportToCsv method, which is responsible for converting the Java objects into CSV format. Within this method, Apache Commons CSV is used to create a CSV file writer and printer.
Top answer
1 of 3
17

It looks like you are printing all the records on the same line .

Other methods like printRecords will be more helpful :

String outputFile = savePath+".csv";
CSVPrinter csvFilePrinter = null;
CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader();
FileWriter fileWriter = new FileWriter(outputFile);
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

csvFilePrinter.printRecords(excelParser.getRecords());


fileWriter.flush();
fileWriter.close();
csvFilePrinter.close();
2 of 3
11

Automatically close & flush

The Answer by Arnaud is correct and good. Here is a variation, shorter and more modern.

Here we:

  • Use the Path, File, and Files classes offered by modern Java NIO.2 to make easier work of file-handling.
  • Use a BufferedWriter for better performance with large amounts of data.
  • Specify the character encoding to be used. Usually UTF-8 is the best. If you do not understand, read this.
  • Include the necessary try-catches for file-related exceptions.
  • Add try-with-resources syntax to auto-close the file.
  • Skip the explicit flushing, as the buffered writer will be flushed automatically as part of auto-closing the BufferedWriter and CSVPrinter. To quote the Javadoc, calling java.io.Writer::close “Closes the stream, flushing it first.”.

Code:

CSVFormat format = CSVFormat.EXCEL.withHeader();
Path path = Paths.get( savePath + ".csv" );
try 
(
    BufferedWriter writer = Files.newBufferedWriter( path , StandardCharsets.UTF_8 ) ;
    CSVPrinter printer = new CSVPrinter( writer , format ) ;
)
{
    printer.printRecords( excelParser.getRecords() );
} 
catch ( IOException e )
{
    e.printStackTrace();
}
// At this point, the `CSVPrinter` is automatically closed. 
// And, ➡️ the `BufferedWriter` is automatically closed and flushed. 
🌐
Baeldung
baeldung.com › home › java › java io › introduction to apache commons csv
Introduction to Apache Commons CSV | Baeldung
January 8, 2024 - FileWriter out = new FileWriter("book_new.csv"); CSVPrinter printer = csvFormat.print(out); We presented the use of Apache’s Commons CSV library through a simple example.
🌐
Stack Overflow
stackoverflow.com › questions › 56943093 › organizing-a-csv-file-with-json-datat
java - Organizing a CSV file with JSON datat - Stack Overflow
So CSV should be set up like: headers: name item1 item2 item3 data cells: [first last] [value1] [value2] [value3] JSON looks like this: { "info":[ { "name":{ "first" : ----- "last":----- }, "item": [ { "item1" : "value1" "item2" : "value2" "item3" : "value3" } ] } I have used the Apache commons FileUtils.writeStringToFile, but that writes all the "items" keys and values in one cell, I need 1 item to 1 cell.
🌐
Apache Commons
commons.apache.org › proper › commons-csv › user-guide.html
User Guide – Apache Commons CSV
Apache Commons, Apache Commons CSV, Apache, the Apache logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
🌐
Javadevcentral
javadevcentral.com › write csv files using apache commons csv
Write CSV Files Using Apache Commons CSV | Java Developer Central
December 24, 2019 - We saw how to write CSV files using Apache Commons CSV. In particular, we looked at how CSVFormat can be configured and the different varieties of the print method on a CSVPrinter that can be used to write CSV data.
🌐
Apache JIRA
issues.apache.org › jira › browse › CSV-174
[CSV-174] Adding methods to support JSON-CSV interchangeability and adding a class/methods to support direct cell modification - ASF Jira
April 21, 2016 - I shall have the following methods in the class: Constructors: void CSVFile(CSVParser); void CSVFile(Object [][],boolean has_headers); void CSVFile(JSON Array,boolean allow_blanks); void CSVFile(String text,String cell_delimiter,String line_delimiter,boolean force_headerSchema);
🌐
Attacomsian
attacomsian.com › blog › read-write-csv-files-apache-commons-csv
How to read and write CSV files using Apache Commons CSV
September 24, 2022 - In this tutorial, you shall learn how to read and write CSV files in Java using Apache Commons CSV.
🌐
GitHub
github.com › TomAlanCarroll › get-json-to-csv › blob › master › src › main › java › com › example › GetJsonToCsv.java
get-json-to-csv/src/main/java/com/example/GetJsonToCsv.java at master · TomAlanCarroll/get-json-to-csv
import org.apache.commons.csv.CSVPrinter; import org.json.JSONArray; import org.json.JSONObject; · import java.io.FileWriter; import java.util.ArrayList; import java.util.List; ·
Author   TomAlanCarroll
Find elsewhere
🌐
Medium
medium.com › javarevisited › boost-your-productivity-at-csv-files-with-apache-commons-csv-in-java-c52b33037c4c
Boost your Production at CSV files with Apache.commons.CSV in Java
July 18, 2020 - Apache Software Foundation gives us the Apache.Commons.CSV library that makes our life easier with CSV files READ/WRITE operations.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-write-json-array-to-csv-file-using-java
How to Write JSON Array to CSV File using Java? - GeeksforGeeks
April 14, 2025 - // Java Program to Write Contents ... check for exceptions try { // Step 1: Reading the contents of the JSON file // using readAllBytes() method and // storing the r...
🌐
Stack Abuse
stackabuse.com › reading-and-writing-csvs-in-java-with-apache-commons-csv
Reading and Writing CSVs in Java with Apache Commons CSV
February 20, 2019 - The Apache Commons CSV library is the Apache Software Foundation's version of a Java CSV parser. According to the project summary, it attempts to "provide a simple interface for reading and writing CSV files of various types".
🌐
CalliCoder
callicoder.com › java-read-write-csv-file-apache-commons-csv
Read / Write CSV files in Java using Apache Commons CSV | CalliCoder
February 18, 2022 - In this article, you'll learn how to Read and Parse CSV files in Java using Apache Commons CSV. You'll also use Apache Commons CSV library to Generate CSV files in Java.
🌐
SpringHow
springhow.com › 🏠 › java › apache commons csv to read and write csv files in java
Apache Commons CSV to Read and Write CSV files in Java | SpringHow
June 29, 2021 - To write this data into file, you just need to create CSVPrinter. And then you can simply start writing records(rows). If you want header row, then make sure you print them above all the data records.
🌐
GitHub
github.com › TransmissionZero › Apache-Commons-CSV-Example
GitHub - TransmissionZero/Apache-Commons-CSV-Example: An example of reading and writing CSV files using the Apache Commons CSV library. · GitHub
An example of reading and writing CSV files using the Apache Commons CSV library. - TransmissionZero/Apache-Commons-CSV-Example
Author   TransmissionZero
🌐
Apache Commons
commons.apache.org › csv
Home – Apache Commons CSV
July 30, 2025 - Apache Commons, Apache Commons CSV, Apache, the Apache logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
🌐
DZone
dzone.com › coding › java › working with csv files in java using apache commons csv
Working With CSV Files in Java Using Apache Commons CSV
April 30, 2018 - The Apache Commons CSV library is a Java library that can be used to read and write CSV files in a very simple and easy way.
🌐
Simplesolution
simplesolution.dev › java-write-read-csv-file-using-apache-commons-csv
Write and Read CSV File in Java using Apache Commons CSV
In this tutorial, we are going to learn how to write and read CSV files in Java applications using Apache Commons CSV library.
🌐
DevGenius
blog.devgenius.io › java-tutorial-read-and-write-using-apache-commons-csv-d737d43f5765
Java Tutorial: Read and Write Using Apache Commons CSV
August 12, 2025 - Learn to read and write CSV files in Java using Apache Commons CSV—a step-by-step guide ideal for data migration, automation, and analytics tasks.
🌐
Stack Abuse
stackabuse.com › reading-and-writing-csv-files-in-kotlin-with-apache-commons
Reading and Writing CSV Files in Kotlin with Apache Commons
February 27, 2023 - In this tutorial, we'll go over how to read and write CSV files in Kotlin, using the Apache Commons library, with examples of reading and writing custom objects.