Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
JasperReports - XLS Data Source Sample (version 3.7.6) |

Shows how the XLS data source implementation could be used to fill reports.
Download All Sample Source Files
Browse Sample Source Files on SVN

Main Features in This Sample
| XLS Data Source
Secondary Features
| Data Sources

top
| XLS Data Source | Documented by Sanda Zaharia
| Description / Goal |  | How to fill a report using data from an XLS file.
| Since |  | 3.6.1
| Other Samples |  |
/demo/samples/datasource
| XLS Data Sources 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. 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 Data Sources section. When reporting data is stored in Microsoft Excel files (XLS), the JRXlsDataSource data source implementation can be used to read it and feed it into the report. The XLS data source uses the `JExcelApi` 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. Report-field mapping for this data source implementation is very similar to the CSV data source field-mapping explained in the CSV Data Source sample. It works on the assumption that the workbook contains data in a tabular form (rows are records and columns contain report-field values). XLS Data Source Example In our example data records are stored in the /data/XlsDataSource.data.xls file. It contains the same records as in the CSV Data Source sample, but the `city` and `id` columns are separated by an empty column (ie. records contain in fact 6 fields, but the second field in each record is always empty). There are no column headers in the .xls file. This means that column names are set independently, as shown in the `getDataSource()` method in the /src/XlsDataSourceApp.java file:
      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;
      }
Column names are the same as in the CSV example: `city`, `id`, `name`, `address` and `state`. But they are associated with particular column indexes: `0, 2, 3, 4, 5`. The empty column's index (1) is skipped, and doing so, the empty content of the second column will be neglected. The `JRXlsDataSource` object prepared above is passed to the engine at fill time (see again the /src/XlsDataSourceApp.java file):
      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));
      }
The `IncludedStates` parameter defined above is used for data filtering. Only records with `Active` or `Trial` states will be taken into account: `<parameter name="IncludedStates" class="java.util.Set"/> ... <filterExpression><![CDATA[$P{IncludedStates}.contains($F{state}) ? Boolean.TRUE : Boolean.FALSE]]></filterExpression>` In order to figure out more on XLS data sources, just test this sample by running from the command line the `ant test view` 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.

© 2001-2010 Jaspersoft Corporation www.jaspersoft.com