PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jasperreports-3.7.4/dist/docs/sample.reference/xlsdatasource/index.html

https://gitlab.com/essere.lab.public/qualitas.class-corpus
HTML | 305 lines | 283 code | 22 blank | 0 comment | 0 complexity | 6777f6641a5669f9160a9219fc265853 MD5 | raw file
  1. <html xmlns:jr="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  2. <head>
  3. <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <title>JasperReports 3.7.4 - XLS Data Source Sample</title>
  5. <style type="text/css">
  6. .title {
  7. font-family: Arial, Verdana, Helvetica, sans-serif;
  8. font-size: 28px;
  9. font-weight: normal;
  10. }
  11. .toc {
  12. font-family: Courier New, Courier, serif;
  13. font-size: 12px;
  14. font-weight: normal;
  15. }
  16. .name {
  17. font-family: Courier New, Courier, serif;
  18. font-size: 16px;
  19. font-weight: bold;
  20. }
  21. .label {
  22. font-family: Arial, Verdana, Helvetica, sans-serif;
  23. font-size: 12px;
  24. font-weight: bold;
  25. font-style: italic;
  26. }
  27. .description {
  28. font-family: Arial, Verdana, Helvetica, sans-serif;
  29. font-size: 12px;
  30. font-weight: normal;
  31. }
  32. .value {
  33. font-family: Courier New, Courier, serif;
  34. font-size: 12px;
  35. font-weight: normal;
  36. }
  37. .element {
  38. font-family: Courier New, Courier, serif;
  39. font-size: 12px;
  40. font-weight: normal;
  41. }
  42. .attribute {
  43. font-family: Courier New, Courier, serif;
  44. font-size: 12px;
  45. font-weight: bold;
  46. }
  47. .code {
  48. font-family: Courier New, Courier, serif;
  49. font-size: 12px;
  50. font-weight: normal;
  51. }
  52. </style>
  53. </head>
  54. <body bgcolor="#FFFFFF">
  55. <a name="top"></a>
  56. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  57. <tr>
  58. <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>
  59. <br>
  60. </td>
  61. </tr>
  62. <tr>
  63. <td colspan="2">
  64. <hr size="1">
  65. </td>
  66. </tr>
  67. <tr valign="middle">
  68. <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>
  69. </tr>
  70. <tr>
  71. <td colspan="2">
  72. <hr size="1">
  73. </td>
  74. </tr>
  75. </table>
  76. <br>
  77. <span class="description"><span class="description">Shows how the XLS data source implementation could be used to fill reports.</span></span>
  78. <br>
  79. <br>
  80. <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>
  81. <br>
  82. <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>
  83. <table cellspacing="0" cellpadding="0" border="0" width="100%">
  84. <tr>
  85. <td style="width: 20px;">
  86. <br>
  87. </td><td>
  88. <br>
  89. </td>
  90. </tr>
  91. <tr>
  92. <td colspan="2"><span class="label">Main Features in This Sample</span></td>
  93. </tr>
  94. <tr>
  95. <td>
  96. <br>
  97. </td><td><span class="element"><a href="#xlsdatasource">XLS Data Source</a></span></td>
  98. </tr>
  99. <tr>
  100. <td colspan="2">
  101. <br>
  102. </td>
  103. </tr>
  104. <tr>
  105. <td colspan="2"><span class="label">Secondary Features</span></td>
  106. </tr>
  107. <tr>
  108. <td></td><td><span class="element"><a href="../datasource/index.html#datasources">Data Sources</a></span></td>
  109. </tr>
  110. </table>
  111. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  112. <tr>
  113. <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%">
  114. <br>
  115. </td>
  116. </tr>
  117. <tr>
  118. <td align="right" colspan="5"><a name="xlsdatasource"></a><a class="toc" href="#top">top</a></td>
  119. </tr>
  120. <tr>
  121. <td colspan="5">
  122. <hr size="1">
  123. </td>
  124. </tr>
  125. <tr valign="top">
  126. <td><img border="0" src="../../resources/jr-16x16.png"></td><td colspan="4"><span class="name">XLS Data Source</span></td>
  127. </tr>
  128. <tr>
  129. <td colspan="5">
  130. <br>
  131. </td>
  132. </tr>
  133. <tr valign="top">
  134. <td>
  135. <br>
  136. </td><td nowrap="true"><span class="label">Description / Goal</span></td><td>
  137. <br>
  138. </td><td colspan="2"><span class="description">
  139. How to fill a report using data from an XLS file.
  140. </span></td>
  141. </tr>
  142. <tr valign="top">
  143. <td>
  144. <br>
  145. </td><td colspan="1"><span class="label">Since</span></td><td>
  146. <br>
  147. </td><td colspan="2"><span class="description">3.6.1</span></td>
  148. </tr>
  149. <tr valign="top">
  150. <td>
  151. <br>
  152. </td><td nowrap="true" colspan="1"><span class="label">Other Samples</span></td><td>
  153. <br>
  154. </td><td colspan="2">
  155. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  156. <tr>
  157. <td><span class="element"><a href="../datasource/index.html">/demo/samples/datasource</a></span></td>
  158. </tr>
  159. </table>
  160. </td>
  161. </tr>
  162. <tr>
  163. <td colspan="5">
  164. <br>
  165. </td>
  166. </tr>
  167. <tr>
  168. <td>
  169. <br>
  170. </td><td colspan="4"><span class="description">
  171. <b>XLS Data Sources</b>
  172. <br>
  173. <br>
  174. Report filling is one of the basic operations during the report generation. After the report compilation,
  175. report data are read from the report data source, and/or calculated from report expressions,
  176. and the generated report sections are filled one by one.
  177. <br>
  178. Data sources are very useful when data come as a set of structured records, either extracted from a
  179. relational database, or loaded from specific files. In order to become more familiar with data source
  180. objects please consult the <a href="../datasources.html#datasources" target="_blank"><span class="element">Data Sources</span></a> section.
  181. <br>
  182. When reporting data is stored in Microsoft Excel files (XLS), the
  183. <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRXlsDataSource.html" target="_blank">JRXlsDataSource</a></span> data source
  184. implementation can be used to read it and feed it into the report.
  185. <br>
  186. The XLS data source uses the <code>JExcelApi</code> library to load the XLS workbook and read
  187. from it. Instances of this data source can be created by supplying either an in-memory
  188. workbook object, a file, or an input stream to read the data from.
  189. <br>
  190. Report-field mapping for this data source implementation is very similar to the CSV data
  191. 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
  192. the workbook contains data in a tabular form (rows are records and columns contain
  193. report-field values).
  194. <br>
  195. <br>
  196. <b>XLS Data Source Example</b>
  197. <br>
  198. <br>
  199. In our example data records are stored in the /data/XlsDataSource.data.xls file. It contains the same
  200. records as in the <a href="../csvdatasource" target="_blank"><span class="element">CSV Data Source</span></a> sample, but the <code>city</code> and
  201. <code>id</code> columns are separated by an empty column (ie. records contain in fact 6 fields, but
  202. the second field in each record is always empty).
  203. <br>
  204. There are no column headers in the .xls file. This means
  205. that column names are set independently, as shown in the
  206. <code>getDataSource()</code> method in the /src/XlsDataSourceApp.java file:
  207. <pre>
  208. private static JRXlsDataSource getDataSource() throws JRException
  209. {
  210. JRXlsDataSource ds;
  211. try
  212. {
  213. String[] columnNames = new String[]{"city", "id", "name", "address", "state"};
  214. int[] columnIndexes = new int[]{0, 2, 3, 4, 5};
  215. ds = new JRXlsDataSource(JRLoader.getLocationInputStream("data/XlsDataSource.data.xls"));
  216. ds.setColumnNames(columnNames, columnIndexes);
  217. }
  218. catch (IOException e)
  219. {
  220. throw new JRException(e);
  221. }
  222. return ds;
  223. }
  224. </pre>
  225. 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>.
  226. 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,
  227. the empty content of the second column will be neglected.
  228. <br>
  229. The <code>JRXlsDataSource</code> object prepared above is passed to the engine at fill time (see again the /src/XlsDataSourceApp.java file):
  230. <pre>
  231. public void fill() throws JRException
  232. {
  233. long start = System.currentTimeMillis();
  234. //Preparing parameters
  235. Map parameters = new HashMap();
  236. parameters.put("ReportTitle", "Address Report");
  237. parameters.put("DataFile", "XlsDataSource.data.xls - XLS data source");
  238. Set states = new HashSet();
  239. states.add("Active");
  240. states.add("Trial");
  241. parameters.put("IncludedStates", states);
  242. JasperFillManager.fillReportToFile("build/reports/XlsDataSourceReport.jasper", parameters, getDataSource());
  243. System.err.println("Filling time : " + (System.currentTimeMillis() - start));
  244. }
  245. </pre>
  246. The <code>IncludedStates</code> parameter defined above is used for data filtering. Only records with <code>Active</code>
  247. or <code>Trial</code> states will be taken into account:
  248. <br>
  249. <br>
  250. <code>
  251. &nbsp;&nbsp;&lt;parameter name="IncludedStates" class="java.util.Set"/&gt;
  252. <br>
  253. <br>
  254. &nbsp;&nbsp;...
  255. <br>
  256. <br>
  257. &nbsp;&nbsp;&lt;filterExpression&gt;&lt;![CDATA[$P{IncludedStates}.contains($F{state}) ? Boolean.TRUE : Boolean.FALSE]]&gt;&lt;/filterExpression&gt;
  258. </code>
  259. <br>
  260. <br>
  261. 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.
  262. 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.
  263. </span></td>
  264. </tr>
  265. <tr>
  266. <td colspan="5">
  267. <br>
  268. </td>
  269. </tr>
  270. </table>
  271. <br>
  272. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  273. <tr>
  274. <td>
  275. <hr size="1">
  276. </td>
  277. </tr>
  278. <tr>
  279. <td align="center"><span style="font-decoration:none;font-family:Verdana,Arial,Helvetica,sans-serif;font-size:8pt;font-style:normal;color:#000000;">&copy; 2001-2010 Jaspersoft Corporation <a style="color:#000000;" target="_blank" href="http://www.jaspersoft.com">www.jaspersoft.com</a></span></td>
  280. </tr>
  281. </table>
  282. </body>
  283. </html>