Java Reference
In-Depth Information
To aid us in CSV parsing, we will use Glen Smith's excellent
OpenCsv library. OpenCsv contains utility classes to ease
the task of working with CSV files. OpenCsv is licensed
under the Apache 2.0 license. It can be downloaded from
http://opencsv.sourceforge.net/ .
Writing a Custom JRDataSource
Implementation
As we have seen from previous examples, all JasperReports datasources
implement the JRDataSource interface. JasperReports also includes the net.
sf.jasperreports.engine.JRRewindableDataSource interface. This interface
extends JRDatasource , adding a single method called moveFirst() . The
moveFirst() method is intended to move the cursor to the first element in the
datasource. Our CsvDataSource will implement JRRewindableDataSource . Let us
take a look at the source code of the CsvDataSource class.
package net.ensode.jasperbook;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRRewindableDataSource;
import au.com.bytecode.opencsv.CSVReader;
public class CsvDataSource implements JRRewindableDataSource
{
private CSVReader csvReader;
private List rows;
private int currentRowIndex = -1;
private int currentColIndex = 0;
private int totalRows;
public CsvDataSource(Reader reader)
{
try
{
csvReader = new CSVReader(reader);
rows = csvReader.readAll();
totalRows = rows.size();
}
 
Search WWH ::




Custom Search