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: DownloadAPI Documentation

Earlier Releases

You can download earlier releases and browse the API documentation, too:

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.

Feedback

Report a bug or request an enhancement…

9 Responses to “CSV/Excel Utility Package for Java”

  1. Ralph’s TechBlog » Blog Archive » CSV Utility Package V1.0.1 released Says:

    [...] new update of my CSV Utility Package was released. It contains some fixes and [...]

  2. Ralph’s TechBlog » Blog Archive » CSV Utility Package V1.0.2 released Says:

    [...] I made some fixes and enhancements to the CSV Utility Package: [...]

  3. Ralph’s TechBlog » Blog Archive » CSV Utility Package 2.0 Beta Says:

    [...] 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 [...]

  4. Audio Services Jobs Says:

    Hey…..nice post!!

    Awesome, No more words to explain :) :) :D just….cool blog.

  5. Ralph’s TechBlog » Blog Archive » CSV/Excel Utility Package V2.0.1 released Says:

    [...] can download it here or visit the Homepage of the utility where you will find some examples on how to use [...]

  6. Insolvenční Rejstřík Says:

    May I mavenize your lib and publish it somewhere?

  7. Ralph Says:

    Hey,

    of course. Just make sure you follow the LGPL license (naming your source)

  8. Insolvenční Rejstřík Says:

    Hi, done, see http://ondrazizka.googlecode.com/svn/maven/cz/dynawest/third/csv/csv/2.0.1/

  9. Insolvenční Rejstřík Says:

    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

Leave a Reply