FAQ

Content


Under what license is the CSV/Excel Utility Package published?

The package is published under the GNU Lesser GPL 3.0. This means that you are free to use the code in your own software projects, private and commercial, without being forced to publish your own source code. However, you need to name the package and its origin along with your software release.

Where can I report bugs or request changes?

There is a JIRA Issue Tracker available where you can report both bugs and changes.

Where do I find a change log?

The Change Log can be found at the Maven Site.

What CSV and Excel formats are supported?

As the CSV implementation is very flexible, it allows you to work with almost any CSV flavour. The default settings applied fit to the Microsoft style (semicolon as column separator). However, you can configure the reader and writer to influence the format (column and row separators, column delimiter, comments).
The Excel classes support all formats up to Excel 2007.

How do I 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();

How do I read an Excel Sheet?

This is basically the same as reading a CSV file but using an ExcelReader:

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();

Can I read specific cells in an Excel Sheet only?

Yes. The ExcelReader provides an interface for reading specific cells only:

Object value = reader.getValue(rowIndex, columnIndex);

Can I select the specific Excel Sheet to read from?

Yes, the ExcelReader provides methods for it:

myExcelReader.selectSheet(sheetIndex);
myExcelReader.selectSheet(sheetName);

How do I 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 control the character encodings?

Version 2.4.0 adds support for character encodings. Just set the charset after you created the reader/writer but before you use it:

csv.TableReader in = new csv.impl.CSVReader(file);
in.setCharset("UTF-8");
...
csv.TableWriter out = new csv.impl.CSVWriter(file);
in.setCharset("UTF-8");
...

If not set explicitely, the implementations will use the defined default charset of your Java engine.

How do I write an Excel Sheet?

Basically, it’s the same technique as writing CSV files. You will use an ExcelWriter:

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();

However, you can make use of many Excel features like writing specific cells or formatting cells.

Can I write specific cells in an Excel Sheet only?

Yes. The ExcelWriter interface provides a method for this:

writer.setValue(rowIndex, columnIndex, myValue);

Can I select the specific Excel Sheet to write to?

Yes, the ExcelWriter has a method for it:

myExcelWriter.selectSheet(sheetIndex);

Or you create a new sheet:

myExcelWriter.createSheet(sheetIndex, sheetName);

How do I modify an existing Excel Sheet?

ExcelReader and ExcelWriter classes provide constructors that need to be used:

// First read the existing Excel sheet
java.io.File f = new java.io.File("excel-test.xls");
csv.impl.ExcelReader in = new csv.impl.ExcelReader(f);
 
// Get the workbook instance
org.apache.poi.ss.usermodel.Workbook workbook = in.getWorkbook();
 
// Create the writer instance with existing workbook
csv.impl.ExcelWriter out = new csv.impl.ExcelWriter(workbook, f);
 
// Modify the Excel sheet here
out.setValue(rowIndex, colIndex, newValue);
out.close();

What 3rd party libraries do I need to work with CSV?

All third party libraries are delivered in /lib folder of the distribution package (The Maven definition has them defined as usual).

How do I convert a CSV file into an Excel Sheet and vice versa?

The CSVUtils class offers various methods to copy one table into another. So you need to pass the readers and writers acordingly:

csv.impl.CSVReader from = new csv.impl.CSVReader("input.csv");
csv.impl.ExcelWriter to = new csv.impl.ExcelWriter("output.xls");
csv.util.CSVUtils.copy(from, to);
 
...
 
csv.impl.ExcelReader from = new csv.impl.ExcelReader("input.xls");
csv.impl.CSVWriter to = new csv.impl.CSVWriter("output.csv");
csv.util.CSVUtils.copy(from, to);

How do I format cells when writing to an Excel Sheet?

ExcelWriter offers the possibility to set an ExcelFormatter implementation. Whenever ExcelWriter writes a value into a cell, this implementation will be called to modify the cell’s style. The DefaultExcelFormatter already takes care of most of the features you would need, such as font sizes, header rows, background color etc. Ideally you would just inherit your own implementation from it and return correct values for these settings.

csv.impl.ExcelWriter writer = new csv.impl.ExcelWriter("myfile.xls");
writer.setFormatter(csv.impl.DefaultExcelFormatter.INSTANCE);

Can I set a Hyperlink for an Excel cell?

Yes. DefaultExcelFormatter already provides a simple method for setting a link of a specific cell. You just need to override this method in your own implementation and return an appropriate hyperlink.

Leave a Reply