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
🌐
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.
Lang
Apache Commons, Apache Commons Lang, Apache, the Apache logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
The Commons Math User Guide
Apache Commons, Apache Commons Math, Apache, the Apache feather logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
🌐
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.
🌐
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%
🌐
Baeldung
baeldung.com › home › java › java io › introduction to apache commons csv
Introduction to Apache Commons CSV | Baeldung
January 8, 2024 - <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.10.0</version> </dependency>
🌐
Apache
projects.apache.org › project.html
Apache Commons CSV - Apache Project Information
This site relies heavily on JavaScript. Please enable it or get a browser that supports it · Managed by the Apache Community Development Project. Copyright© the Apache Software Foundation. Licensed under the Apache License, Version 2.0 Apache® and the Apache logo are trademarks of The Apache ...
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. 
🌐
Medium
medium.com › @kaveeshapiumini1999 › data-export-using-apache-commons-csv-09a154243d97
Data Export using Apache Commons CSV | by Kaveeshapiumini | Medium
January 4, 2024 - Data Export using Apache Commons CSV CSV (Comma-Separated Values) files are commonly used for storing tabular data, and Apache Commons CSV makes it easier for Java applications to handle such data in …
🌐
Maven Central
central.sonatype.com › artifact › org.apache.commons › commons-csv › 1.8
Maven Central: org.apache.commons:commons-csv:1.8
The Apache Commons CSV library provides a simple interface for reading and writing CSV files of various types.
Find elsewhere
🌐
Fedora
packages.fedoraproject.org › pkgs › apache-commons-csv
apache-commons-csv - Fedora Packages
apache-commons-csv - Utilities to assist with handling of CSV files · apache-commons-csv-javadoc - API documentation for apache-commons-csv ·
🌐
Gary Gregory
garygregory.wordpress.com › 2015 › 08 › 25 › apache-commons-csv-releases-version-1-2
Apache Commons CSV releases version 1.2 | Gary Gregory
August 25, 2015 - The Apache Commons CSV team is pleased to announce the 1.2 release! The Apache Commons CSV library provides a simple interface for reading and writing CSV files of various types. This is our third release.
🌐
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.
🌐
Apache Commons
commons.apache.org › proper › commons-csv › apidocs › org › apache › commons › csv › CSVFormat.html
CSVFormat (Apache Commons CSV 1.14.2-SNAPSHOT API)
Package org.apache.commons.csv · java.lang.Object · org.apache.commons.csv.CSVFormat · All Implemented Interfaces: Serializable · public final class CSVFormat extends Object implements Serializable · Specifies the format of a CSV file for parsing and writing.
🌐
Javadoc.io
javadoc.io › doc › org.apache.commons › commons-csv › 1.7 › index.html
commons-csv 1.7 javadoc (org.apache.commons)
Latest version of org.apache.commons:commons-csv · https://javadoc.io/doc/org.apache.commons/commons-csv · Current version 1.7 · https://javadoc.io/doc/org.apache.commons/commons-csv/1.7 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/org.apache.co...
🌐
Frequal
frequal.com › java › ApacheCommonsCsvForEasyJavaCsvParsing.html
Apache Commons CSV for Easy CSV Parsing in Java
April 1, 2025 - To parse CSV files in Java, Apache Commons CSV should be your go-to library.
🌐
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.
🌐
Sonatype OSS Index
ossindex.sonatype.org › maven › org.apache.commons/commons-csv
Maven - org.apache.commons/commons-csv - Sonatype OSS Index
Find vulnerabilities, licenses, and versions for org.apache.commons/commons-csv : The Apache Commons CSV library provides a simple interface for reading and writing CSV files of various types.
🌐
DevGenius
blog.devgenius.io › easily-parse-and-generate-csv-files-6dad666fef41
Easily parse and generate CSV files | by HBLOG | Dev Genius
December 2, 2024 - Commons CSV provides a set of simple and powerful APIs to help developers easily parse and generate CSV files. The core principle of Apache Commons CSV is based on streaming and iterator mode. It reads CSV files line by line through a parser (CSVParser) and parses each line into a CSVRecord object.
🌐
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