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
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.
🌐
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 - This library provides a simple interface to read and write CSV files of various types. The Apache Commons CSV library is well-maintained and updated regularly.
🌐
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.
🌐
Medium
medium.com › @kaveeshapiumini1999 › data-export-using-apache-commons-csv-09a154243d97
Data Export using Apache Commons CSV | by Kaveeshapiumini | Medium
January 4, 2024 - Within this method, Apache Commons CSV is used to create a CSV file writer and printer. Each Person object’s data is iterated through, and the CSVPrinter is used to write corresponding records to the CSV file.
🌐
Apache Commons
commons.apache.org › proper › commons-csv
Home – 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.
🌐
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 - compile "org.apache.commons:commons-csv:1.5" Let's start by generating a simple CSV file — student.csv — in the following program. Our program to generate a simple CSV file: import java.io.IOException; import java.io.Writer; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; public class BasicCsvWriter { public static void main(String[] args) { try { //We have to create the CSVPrinter class object Writer writer = Files.newBufferedWriter(Paths.get("student.csv")); CSVPrinter csvP
🌐
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 - Reading or writing a CSV file is a very common use-case that Java developers encounter in their day-to-day work. If you need a simple way to read a CSV file or generate a new one for your project then this blog post is for you. In this post, You’ll learn how to read and write CSV files in Java using a very simple open source library called Apache Commons CSV.
Find elsewhere
🌐
GitHub
github.com › apache › commons-csv
GitHub - apache/commons-csv: Apache Commons CSV · GitHub
The Apache Commons CSV library provides a simple interface for reading and writing CSV files of various types.
Starred by 412 users
Forked by 304 users
Languages   Java 99.8% | Shell 0.2%
🌐
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 - Apache Commons CSV is a java library that reads and writes files in Comma Separated Value (CSV) format. This library can write in all supported variations of CSV files like MSExcel, Informix, Oracle, MySQL and TSV(Tab-separated values).
🌐
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.
🌐
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 - As with all libraries associated with Apache, it operates with an Apache license, meaning it can be used, distributed and modified freely. Apache Commons allows developers to define their own formats, but offers a predefined set of formats with its CSVFormat class.
🌐
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.
🌐
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
🌐
Google Sites
sites.google.com › view › downloadutyuxm › Apache-Commons-Csv-Writer-Example
Apache Commons Csv Writer Example
ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ Select Download Format Apache Commons Csv Writer Example Download Apache Commons Csv Writer Example PDF Download Apache Commons Csv Writer Example DOC Shorter and thrown exception other csv files we do i create carbondata file by apache commons csv
🌐
Apache Commons
commons.apache.org › proper › commons-csv › apidocs › index.html
Overview (Apache Commons CSV 1.14.2-SNAPSHOT API)
Apache Commons CSV reads and writes files in variations of the Comma Separated Value (CSV) format.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › apache › commons › csv
Write/Read CSV Files with Apache Commons CSV Example - Java Code Geeks
November 9, 2023 - However, this example will illustrate how to write and read CSV files with an open source 3rd Party tool “Apache Commons CSV”, Apache Commons CSV library provides a simple interface for reading and writing CSV files of various types.
🌐
Maven Repository
mvnrepository.com › artifact › org.apache.commons › commons-csv
Maven Repository: org.apache.commons » commons-csv
July 27, 2025 - The Apache Commons CSV library provides a simple interface for reading and writing CSV files of various types.
🌐
Apache Commons
commons.apache.org › proper › commons-csv › apidocs › org › apache › commons › csv › CSVPrinter.html
CSVPrinter (Apache Commons CSV 1.14.2-SNAPSHOT API)
Package org.apache.commons.csv · java.lang.Object · org.apache.commons.csv.CSVPrinter · All Implemented Interfaces: Closeable, Flushable, AutoCloseable · public final class CSVPrinter extends Object implements Flushable, Closeable · Prints values in a CSV format.
🌐
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 - Similar to reading files, we can also write CSV files using Apache Commons. This time around, we'll be using the CSVPrinter. Just how the CSVReader accepts a BufferedReader, the CSVPrinter accepts a BufferedWriter, and the CSVFormat we'd like it to use while writing the file.