/projects/jasperreports-3.7.4/dist/docs/sample.reference/xlsdatasource/index.html
HTML | 305 lines | 283 code | 22 blank | 0 comment | 0 complexity | 6777f6641a5669f9160a9219fc265853 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 - XLS 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 - XLS 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 XLS 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/xlsdatasource/?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="#xlsdatasource">XLS 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="xlsdatasource"></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">XLS 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 an XLS 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">3.6.1</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>XLS Data Sources</b>
- <br>
- <br>
- Report filling is one of the basic operations during the report generation. After the report compilation,
- report data are read from the report data source, and/or calculated from report expressions,
- and the generated report sections are filled one by one.
- <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>
- When reporting data is stored in Microsoft Excel files (XLS), the
- <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRXlsDataSource.html" target="_blank">JRXlsDataSource</a></span> data source
- implementation can be used to read it and feed it into the report.
- <br>
- The XLS data source uses the <code>JExcelApi</code> library to load the XLS workbook and read
- from it. Instances of this data source can be created by supplying either an in-memory
- workbook object, a file, or an input stream to read the data from.
- <br>
- Report-field mapping for this data source implementation is very similar to the CSV data
- source field-mapping explained in the <a href="../csvdatasource" target="_blank"><span class="element">CSV Data Source</span></a> sample. It works on the assumption that
- the workbook contains data in a tabular form (rows are records and columns contain
- report-field values).
- <br>
- <br>
- <b>XLS Data Source Example</b>
- <br>
- <br>
- In our example data records are stored in the /data/XlsDataSource.data.xls file. It contains the same
- records as in the <a href="../csvdatasource" target="_blank"><span class="element">CSV Data Source</span></a> sample, but the <code>city</code> and
- <code>id</code> columns are separated by an empty column (ie. records contain in fact 6 fields, but
- the second field in each record is always empty).
- <br>
- There are no column headers in the .xls file. This means
- that column names are set independently, as shown in the
- <code>getDataSource()</code> method in the /src/XlsDataSourceApp.java file:
- <pre>
- private static JRXlsDataSource getDataSource() throws JRException
- {
- JRXlsDataSource ds;
- try
- {
- String[] columnNames = new String[]{"city", "id", "name", "address", "state"};
- int[] columnIndexes = new int[]{0, 2, 3, 4, 5};
- ds = new JRXlsDataSource(JRLoader.getLocationInputStream("data/XlsDataSource.data.xls"));
- ds.setColumnNames(columnNames, columnIndexes);
- }
- catch (IOException e)
- {
- throw new JRException(e);
- }
- return ds;
- }
- </pre>
- Column names are the same as in the CSV example: <code>city</code>, <code>id</code>, <code>name</code>, <code>address</code> and <code>state</code>.
- But they are associated with particular column indexes: <code>0, 2, 3, 4, 5</code>. The empty column's index (1) is skipped, and doing so,
- the empty content of the second column will be neglected.
- <br>
- The <code>JRXlsDataSource</code> object prepared above is passed to the engine at fill time (see again the /src/XlsDataSourceApp.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", "XlsDataSource.data.xls - XLS data source");
- Set states = new HashSet();
- states.add("Active");
- states.add("Trial");
- parameters.put("IncludedStates", states);
- JasperFillManager.fillReportToFile("build/reports/XlsDataSourceReport.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 XLS 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>