/projects/jasperreports-3.7.4/dist/docs/sample.reference/csvdatasource/index.html
HTML | 365 lines | 338 code | 27 blank | 0 comment | 0 complexity | 834ea13bf5ae514acea023b5904e6179 MD5 | raw file
- <html xmlns:jr="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <head>
- <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JasperReports 3.7.4 - CSV Data Source Sample</title>
- <style type="text/css">
- .title {
- font-family: Arial, Verdana, Helvetica, sans-serif;
- font-size: 28px;
- font-weight: normal;
- }
- .toc {
- font-family: Courier New, Courier, serif;
- font-size: 12px;
- font-weight: normal;
- }
- .name {
- font-family: Courier New, Courier, serif;
- font-size: 16px;
- font-weight: bold;
- }
- .label {
- font-family: Arial, Verdana, Helvetica, sans-serif;
- font-size: 12px;
- font-weight: bold;
- font-style: italic;
- }
- .description {
- font-family: Arial, Verdana, Helvetica, sans-serif;
- font-size: 12px;
- font-weight: normal;
- }
- .value {
- font-family: Courier New, Courier, serif;
- font-size: 12px;
- font-weight: normal;
- }
- .element {
- font-family: Courier New, Courier, serif;
- font-size: 12px;
- font-weight: normal;
- }
- .attribute {
- font-family: Courier New, Courier, serif;
- font-size: 12px;
- font-weight: bold;
- }
- .code {
- font-family: Courier New, Courier, serif;
- font-size: 12px;
- font-weight: normal;
- }
- </style>
- </head>
- <body bgcolor="#FFFFFF">
- <a name="top"></a>
- <table width="100%" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <td align="right" colspan="2"><span class="element"><a href="../../sample.reference.html">Sample Reference</a> - <a href="../../schema.reference.html">Schema Reference</a> - <a href="../../config.reference.html">Configuration Reference</a> - <a href="http://jasperreports.sourceforge.net/api/index.html">API (Javadoc)</a></span>
- <br>
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <hr size="1">
- </td>
- </tr>
- <tr valign="middle">
- <td nowrap="true"><span class="title">JasperReports - CSV Data Source Sample (version 3.7.4)</span></td><td align="right"><img border="0" src="../../resources/jasperreports.png"></td>
- </tr>
- <tr>
- <td colspan="2">
- <hr size="1">
- </td>
- </tr>
- </table>
- <br>
- <span class="description"><span class="description">Shows how the CSV data source implementation could be used to fill reports.</span></span>
- <br>
- <br>
- <span class="element"><a href="http://sourceforge.net/projects/jasperreports/files/jasperreports/JasperReports%203.7.4/jasperreports-3.7.4-project.zip/download" target="_blank">Download All Sample Source Files</a></span>
- <br>
- <span class="element"><a href="http://jasperforge.org/scm/viewvc.php/tags/jr-3-7-4/jasperreports/demo/samples/csvdatasource/?root=jasperreports" target="_blank">Browse Sample Source Files on SVN</a></span>
- <table cellspacing="0" cellpadding="0" border="0" width="100%">
- <tr>
- <td style="width: 20px;">
- <br>
- </td><td>
- <br>
- </td>
- </tr>
- <tr>
- <td colspan="2"><span class="label">Main Features in This Sample</span></td>
- </tr>
- <tr>
- <td>
- <br>
- </td><td><span class="element"><a href="#csvdatasource">CSV Data Source</a></span></td>
- </tr>
- <tr>
- <td colspan="2">
- <br>
- </td>
- </tr>
- <tr>
- <td colspan="2"><span class="label">Secondary Features</span></td>
- </tr>
- <tr>
- <td></td><td><span class="element"><a href="../datasource/index.html#datasources">Data Sources</a></span></td>
- </tr>
- </table>
- <table border="0" cellpadding="0" cellspacing="0" width="100%">
- <tr>
- <td><img height="1" width="20" border="0" src="../../resources/px.gif"></td><td><img height="1" width="20" border="0" src="../../resources/px.gif"></td><td><img height="1" width="20" border="0" src="../../resources/px.gif"></td><td><img height="1" width="20" border="0" src="../../resources/px.gif"></td><td width="100%">
- <br>
- </td>
- </tr>
- <tr>
- <td align="right" colspan="5"><a name="csvdatasource"></a><a class="toc" href="#top">top</a></td>
- </tr>
- <tr>
- <td colspan="5">
- <hr size="1">
- </td>
- </tr>
- <tr valign="top">
- <td><img border="0" src="../../resources/jr-16x16.png"></td><td colspan="4"><span class="name">CSV Data Source</span></td>
- </tr>
- <tr>
- <td colspan="5">
- <br>
- </td>
- </tr>
- <tr valign="top">
- <td>
- <br>
- </td><td nowrap="true"><span class="label">Description / Goal</span></td><td>
- <br>
- </td><td colspan="2"><span class="description">
- How to fill a report using data from a CSV file.
- </span></td>
- </tr>
- <tr valign="top">
- <td>
- <br>
- </td><td colspan="1"><span class="label">Since</span></td><td>
- <br>
- </td><td colspan="2"><span class="description">1.2.0</span></td>
- </tr>
- <tr valign="top">
- <td>
- <br>
- </td><td nowrap="true" colspan="1"><span class="label">Other Samples</span></td><td>
- <br>
- </td><td colspan="2">
- <table border="0" cellpadding="0" cellspacing="0" width="100%">
- <tr>
- <td><span class="element"><a href="../datasource/index.html">/demo/samples/datasource</a></span></td>
- </tr>
- </table>
- </td>
- </tr>
- <tr>
- <td colspan="5">
- <br>
- </td>
- </tr>
- <tr>
- <td>
- <br>
- </td><td colspan="4"><span class="description">
- <b>CSV Data Sources</b>
- <br>
- <br>
- Report filling is one of the basic steps during the report generation. After the report compilation,
- significant report data are read from the report data source, or calculated from report expressions,
- and the generated <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperReport.html" target="_blank">JasperReport</a></span> object
- is filled section by section.
- <br>
- Data sources are very useful when data come as a set of structured records, either extracted from a
- relational database, or loaded from specific files. In order to become more familiar with data source
- objects please consult the <a href="../datasources.html#datasources" target="_blank"><span class="element">Data Sources</span></a> section.
- <br>
- Sometimes data that users need to fill the report with is found in plain text files, in a
- certain format, such as the popular CSV (comma-separated value).
- <br>
- JasperReports provides an implementation for such a data source, by wrapping the CSV
- data from a text file into a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRCsvDataSource.html" target="_blank">JRCsvDataSource</a></span>.
- The CSV data source usually needs to read a file from disk, or at least from an input stream.
- <br>
- CSV is a file format with very few formatting rules: data rows are separated by a record
- delimiter (text sequence) and fields inside each row are separated by a field delimiter
- (character). Fields containing delimiter characters can be placed inside quotes. If fields
- contain quotes themselves, these are duplicated (for example, <code>"John ""Doe"""</code> will be
- displayed as <code>John "Doe"</code>).
- <br>
- The most common delimiters for CSV files are:
- <ul>
- <li>comma (<code>,</code>) - as field delimiter</li>
- <li>newline (<code>\n</code>) - as record delimiter</li>
- </ul>
- Users can override these default values by calling <code>setFieldDelimiter(char)</code> and
- <code>setRecordDelimiter(String)</code> of the <code>JRCsvDataSource</code> class.
- <br>
- There are two categories of CSV files:
- <ol>
- <li>files with a header record containing column names, all the other records containing field values;</li>
- <li>files without a header record; all records contain only field values. This is the default category.</li>
- </ol>
- For the files in the first category, column names are read from the first row in the CSV file.
- But the engine should be informed that the input file belongs to the
- first category. This can be done by calling the <code>setUseFirstRowAsHeader(true)</code> method of the <code>JRCsvDataSource</code>.
- <br>
- If files belong to the second category (default), column names have to be provided separately, taking into account that
- the column names order should be the same as the column order in a data record. If this is not possible,
- the default naming convention is to use the <code>COLUMN_</code> prefix followed by the zero-based column index.
- <br>
- For instance, if is known that a particular column is the third column in the record
- (index=2), then one could name the corresponding field "COLUMN_2" and use the
- column data without problems.
- <br>
- Another problem when working with CSV files is related to data types. Handling data types
- for fields in CSV data sources is special since the CSV file format
- does not provide such information. This kind of matter is solved by trying to match each field in
- the data source to its corresponding report field type. For number and date/time fields,
- converting text values to <code>java.lang.Number</code> and <code>java.util.Date</code> values respectively
- requires parsing using format objects. This is controlled by specifying the date and
- number format objects to be used with the <code>JRCsvDataSource</code> instance by calling its
- <code>setDateFormat(DateFormat)</code> and <code>setNumberFormat(NumberFormat)</code> methods
- before passing it to the report-filling process.
- <br>
- <br>
- <b>CSV Data Source Example</b>
- <br>
- <br>
- In our example data records are stored in the /data/CsvDataSource.txt file. Although the file extension is <code>.txt</code>,
- it contains structured data according to the CSV rules:
- <pre>
- "Dallas",47,"Janet Fuller","445 Upland Pl.","Trial"
- "Lyon",38,"Andrew Heiniger","347 College Av.","Active"
- "Dallas",43,"Susanne Smith","2 Upland Pl.","Active"
- "Berne",22,"Bill Ott","250 - 20th Ave.","Active"
- "Boston",32,"Michael Ott","339 College Av.","Trial"
- "Dallas",4,"Sylvia Ringer","365 College Av.","Active"
- "Boston",23,"Julia Heiniger","358 College Av.","Active"
- "Chicago",39,"Mary Karsen","202 College Av.","Active"
- "Dallas",40,"Susanne Miller","440 - 20th Ave.","Trial"
- "Berne",9,"James Schneider","277 Seventh Av.","Active"
- "Dallas",36,"John Steel","276 Upland Pl.","Suspended"
- "Chicago",35,"George Karsen","412 College Av.","Suspended"
- "Dallas",37,"Michael Clancy","19 Seventh Av.","Deleted"
- "Lyon",2,"Anne Miller","20 Upland Pl.","Active"
- "Dallas",0,"Laura Steel","429 Seventh Av.","Active"
- "Lyon",28,"Susanne White","74 - 20th Ave.","Deleted"
- "Paris",5,"Laura Miller","294 Seventh Av.","Active"
- "Lyon",17,"Laura Ott","443 Seventh Av.","Active"
- "New York",46,"Andrew May","172 Seventh Av.","Active"
- "New York",44,"Sylvia Ott","361 College Av.","Active"
- "Dallas",19,"Susanne Heiniger","86 - 20th Ave.","Active"
- "Chicago",11,"Julia White","412 Upland Pl.","Active"
- "Dallas",10,"Anne Fuller","135 Upland Pl.","Active"
- "New York",41,"Bill King","546 College Av.","Deleted"
- "Oslo",45,"Janet May","396 Seventh Av.","Active"
- "Paris",18,"Sylvia Fuller","158 - 20th Ave.","Trial"
- "San Francisco",48,"Robert White","549 Seventh Av.","Active"
- "Paris",25,"Sylvia Steel","269 College Av.","Suspended"
- "San Francisco",7,"James Peterson","231 Upland Pl.","Active"
- "Oslo",42,"Robert Ott","503 Seventh Av.","Trial"
- </pre>
- The file has no header row with column names. Column names are set independently, as shown in the
- <code>getDataSource()</code> method in the /src/CsvDataSourceApp.java file:
- <pre>
- private static JRCsvDataSource getDataSource() throws JRException
- {
- String[] columnNames = new String[]{"city", "id", "name", "address", "state"};
- JRCsvDataSource ds = new JRCsvDataSource(JRLoader.getLocationInputStream("data/CsvDataSource.txt"));
- ds.setRecordDelimiter("\r\n");
- ds.setColumnNames(columnNames);
- return ds;
- }
- </pre>
- The five column names are: <code>city</code>, <code>id</code>, <code>name</code>, <code>address</code> and <code>state</code>,
- in this particular order. Field names are identical to the column names defined here.
- <br>
- The <code>JRCsvDataSource</code> object prepared above is passed to the engine at fill time (see again the /src/CsvDataSourceApp.java file):
- <pre>
- public void fill() throws JRException
- {
- long start = System.currentTimeMillis();
- //Preparing parameters
- Map parameters = new HashMap();
- parameters.put("ReportTitle", "Address Report");
- parameters.put("DataFile", "CsvDataSource.txt - CSV data source");
- Set states = new HashSet();
- states.add("Active");
- states.add("Trial");
- parameters.put("IncludedStates", states);
- JasperFillManager.fillReportToFile("build/reports/CsvDataSourceReport.jasper", parameters, getDataSource());
- System.err.println("Filling time : " + (System.currentTimeMillis() - start));
- }
- </pre>
- The <code>IncludedStates</code> parameter defined above is used for data filtering. Only records with <code>Active</code>
- or <code>Trial</code> states will be taken into account:
- <br>
- <br>
- <code>
- <parameter name="IncludedStates" class="java.util.Set"/>
- <br>
- <br>
- ...
- <br>
- <br>
- <filterExpression><![CDATA[$P{IncludedStates}.contains($F{state}) ? Boolean.TRUE : Boolean.FALSE]]></filterExpression>
- </code>
- <br>
- <br>
- In order to figure out more on CSV data sources, just test this sample by running from the command line the <code>ant test view</code> command.
- It will generate all supported document types containing the sample report in the /build/reports directory, and then the report will be open with JasperReports internal viewer.
- </span></td>
- </tr>
- <tr>
- <td colspan="5">
- <br>
- </td>
- </tr>
- </table>
- <br>
- <table width="100%" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <td>
- <hr size="1">
- </td>
- </tr>
- <tr>
- <td align="center"><span style="font-decoration:none;font-family:Verdana,Arial,Helvetica,sans-serif;font-size:8pt;font-style:normal;color:#000000;">© 2001-2010 Jaspersoft Corporation <a style="color:#000000;" target="_blank" href="http://www.jaspersoft.com">www.jaspersoft.com</a></span></td>
- </tr>
- </table>
- </body>
- </html>