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 are deprecated since V2.0 and CSVReader and CSVWriter classes are moved into other packages in favour of readability and structuring of the classes.
Excel functionality is available since version 2.0.
Download
Stable Release
The most recent stable version is V2.0.1. You can download it here. 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.
V2.0.1: Download – API Documentation
Earlier Releases
You can download earlier releases and browse the API documentation, too:
- V2.0.: Download – API Documentation
- V1.0.1: Download – API Documentation
- V1.0.2: Download – API Documentation
- V1.0: Download – API Documentation
How to read a CSV file?
This code snippet demonstrates the use of CSVReader.
java.io.File f = new java.io.File("csv-test.csv"); csv.TableReader in = new csv.impl.CSVReader(f); while (in.hasNext()) { Object columns[] = in.next(); // Do something here } in.close();
Please note that the CSVReader class actually implements the Iterator interface.
How to read an Excel Sheet?
This is basically the same as reading a CSV file:
java.io.File f = new java.io.File("excel-test.xls"); csv.TableReader in = new csv.impl.ExcelReader(f); while (in.hasNext()) { Object columns[] = in.next(); // Do something here } in.close();
How to write a CSV file?
This code snippet demonstrates the use of CSVWriter.
java.io.File f = new java.io.File("csv-test.csv"); csv.TableWriter out = new csv.impl.CSVWriter(f); out.printRow(new Object[] {"0:0", "0:1", "0:2"); out.printRow(new Object[] {"1:0", "1:1", "1:2"); out.close();
How do I write an Excel Sheet?
Basically, it’s the same technique as writing CSV files:
java.io.File f = new java.io.File("excel-test.xls"); csv.TableWriter out = new csv.impl.ExcelWriter(f); out.printRow(new Object[] {"0:0", "0:1", "0:2"); out.printRow(new Object[] {"1:0", "1:1", "1:2"); out.close();
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 [...]
May 21st, 2010 at 9:57 am
Hey…..nice post!!
Awesome, No more words to explain
just….cool blog.
June 15th, 2010 at 9:44 am
[...] can download it here or visit the Homepage of the utility where you will find some examples on how to use [...]
July 23rd, 2010 at 8:14 pm
May I mavenize your lib and publish it somewhere?
July 23rd, 2010 at 11:10 pm
Hey,
of course. Just make sure you follow the LGPL license (naming your source)
July 28th, 2010 at 5:36 pm
Hi, done, see http://ondrazizka.googlecode.com/svn/maven/cz/dynawest/third/csv/csv/2.0.1/
July 28th, 2010 at 5:38 pm
Regarding LGPL – Maven way of referencing the source is adding project’s URL to the POM, which is then used for eventual reports and generated site. Is that OK?
By the way, I created a related tool, see here:
http://ondra.zizka.cz/stranky/programovani/ruzne/querying-transforming-csv-using-sql.texy