CSV/Excel Utility Package for Java
Unfortunately, Java does not offer any methods to simply read CSV files or produce such, not mentioning Excel. I for myself found it quite easy to work with such files; especially when you need to deal with data from and to Microsoft Excel.
My CSV/Excel Utility Package, published under the GNU Lesser General Public License, allows you to easily integrate CSV and Excel functionality into your application, just by using Iterator-like classes for reading, and PrintStream-like classes for writing. The CSV tools can be configured to use different column delimiter and separator characters in case you need to adopt some other versions of CSV. The default configuration conforms to the Excel style of CSV.
The Excel tools conform to the same way that CSV tools behave (see below). Therefore, two new interfaces TableReader and TableWriter were introduced to reflect the common functions. The new ExcelWriter allows you to easily create Excel files while still having the flexibility of formatting issues (see ExcelFormatter interface). The implementation is based on Apache’s POI library.
Since this CSV/Excel package uses streams, you are able to read from any stream. And, of course, you can write to any stream. You could even synchronize within your application by applying the reader/writer synchronization described in one of my articles.
Please notice that some methods were deprecated with V2.0 and CSVReader and CSVWriter classes were moved into other packages in favour of readability and structuring of the classes.
Excel functionality is available since version 2.0.
Download
Stable Release
You can download the latest stable release here (V2.0). It includes the source files along with an Ant build file, the API documentation and the ready-to-use binary JAR file. You could also browse the Subversion repository and the API documentation. There is also a FAQ available which explains most common tasks by examples.
Earlier Releases
You can download earlier releases and browse the API documentation, too:
- V1.0: Download – API Documentation
- V1.0.1: Download – API Documentation
- V1.0.2: Download – API Documentation
How to read a CSV file?
Here is a short example on reading a CSV file using the CSVReader class:
java.io.File f = new java.io.File("csv-test.csv"); csv.impl.CSVReader in = new csv.impl.CSVReader(f); while (in.hasNext()) { String columns[] = in.next(); // Do something here } in.close();
Please note that the CSVReader class actually implements the Iterator interface.
How to write a CSV file?
Writing a CSV file is even easier. Just create an instance of the CSVWriter class and pass it your rows:
java.io.File f = new java.io.File("csv-test.csv"); csv.impl.CSVWriter out = new csv.impl.CSVWriter(f); out.printRow(new String[] { "0:0", "0:1", "0:2" }); out.printRow(new String[] { "1:0", "1:1", "1:2" }); out.close();
How to read/write comments?
You need to register a CommentCallback with the CSVReader in order to get notified about comments:
... in.addCommentCallback(new MyCSVCommentCallback()); ...
Your callback will then be notified each time a comment is discovered in the CSV file.
Writing a comment is as easy as writing rows:
out.printComment("This is a comment");
Documentation
The API documentation tells you all details and configuration parameters that you can use to control the behaviour of the reader and writer classes.
September 28th, 2009 at 4:38 pm
[...] new update of my CSV Utility Package was released. It contains some fixes and [...]
October 20th, 2009 at 9:37 am
[...] I made some fixes and enhancements to the CSV Utility Package: [...]
November 26th, 2009 at 1:25 pm
[...] work has been spent over the last weeks to upgrade the stable CSV Utility Package. Of course, the new version contains all the useful existing functionality. The most benefitial [...]