gettingstartedwithjasperreports
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| gettingstartedwithjasperreports [2022/01/05 16:16] – [Global Report Query] wikiadmin | gettingstartedwithjasperreports [2022/01/05 20:09] (current) – [Generate a Spreadsheet from SimpleReport] wikiadmin | ||
|---|---|---|---|
| Line 14: | Line 14: | ||
| You will need the SQLite driver jar included in the [[databasereferencecheatsheet# | You will need the SQLite driver jar included in the [[databasereferencecheatsheet# | ||
| - | ====Simple Report Example==== | + | ====Simple Report |
| + | |||
| + | This tutorial will show you how to design and test a simple table report in Jaspersoft Studio which uses a SQLite database for the table data. | ||
| ===Jaspersoft Studio Startup=== | ===Jaspersoft Studio Startup=== | ||
| Line 22: | Line 24: | ||
| ===Data Adapters=== | ===Data Adapters=== | ||
| - | The workspace contains projects and data adapters. Data Adapters are the connections to databases (and sometimes other data sources) for the report. When you first start up Jaspersoft Studio you will see two items under Data Adapters: " | + | The workspace contains projects and data adapters. Data Adapters are the connections to databases (and sometimes other data sources) for the report. When you first start up Jaspersoft Studio you will see two items under Data Adapters: " |
| To add the SQLite database as a Data Adapter, right click on Data Adapters and select " | To add the SQLite database as a Data Adapter, right click on Data Adapters and select " | ||
| Line 116: | Line 118: | ||
| <note important> | <note important> | ||
| - | Every time you make a change to the report design, the project should be saved. The project is automatically saved every five minutes | + | Every time you make a change to the report design, the project should be saved. The project is never automatically saved by the Jaspersoft Studio application. Whenever you see a " |
| Line 122: | Line 124: | ||
| </ | </ | ||
| + | ===Global Report Query=== | ||
| + | |||
| + | The Jasper Report " | ||
| + | |||
| + | Right click on the main report node " | ||
| + | |||
| + | {{: | ||
| + | |||
| + | The Dataset and Query panel will popup. Be sure that the exported connection " | ||
| + | |||
| + | {{: | ||
| + | |||
| + | |||
| + | Press OK to finish this setup and close the Dataset and Query Dialog panel. | ||
| ===Adding a Table to the Simple Report=== | ===Adding a Table to the Simple Report=== | ||
| Line 219: | Line 235: | ||
| It is recommended that you add your jasper report definitions to the same code repository you use for other projects so that you can recover the file and track changes as needed. The two files you should save for this project are the SimpleReport.jrxml and SQLiteTestDatabase.xml, | It is recommended that you add your jasper report definitions to the same code repository you use for other projects so that you can recover the file and track changes as needed. The two files you should save for this project are the SimpleReport.jrxml and SQLiteTestDatabase.xml, | ||
| </ | </ | ||
| + | |||
| + | ====Tutorial 2: Running the SimpleReport in MIStudio==== | ||
| + | |||
| + | This is an extension of the Tutorial 1: Simple Report Design and Test Example. If you want to skip Tutorial 1, you can get the set of files needed to run Tutorial 2 [[https:// | ||
| + | |||
| + | The purpose of this Tutorial is to show you how to use the compiled SimpleReport JasperReport project built with Jaspersoft Studio in Tutorial 1 to generate a PDF report in MIStudio. | ||
| + | |||
| + | ===Setting Up=== | ||
| + | |||
| + | If you do not have MIStudio Plus Reporting installed please do this step first. | ||
| + | |||
| + | You will need these files from the SimpleReport project in Tutorial 1: the compiled report SimpleReport.jasper, | ||
| + | |||
| + | You will need to edit the SQLiteTestDatabase.xml file in a text editor (Notepad++ or similar) to remove the hard-coded paths to the database file and the driver jar. It should look similar to this, with the correct name for your SQLite driver jar: | ||
| + | |||
| + | <code xml> | ||
| + | <?xml version=" | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | You will also need the DynamicJasper.jar in your MIStudio lib directory, which is installed with the Jasper Reports module for MIStudio. | ||
| + | |||
| + | ===MIStudio SimpleReport Project=== | ||
| + | |||
| + | Start MIStudio and create a new project, SimpleReport. Add a button to the design window and copy this to the diagram window. Label this button " | ||
| + | |||
| + | Copy and paste this script to the JavaScript bean: | ||
| + | |||
| + | <code javascript> | ||
| + | |||
| + | //Script to generate a PDF report from SimpleReport.jasper | ||
| + | |||
| + | var HashMap = Java.type(" | ||
| + | var FileInputStream = Java.type(" | ||
| + | var JasperPrint = Java.type(" | ||
| + | var JasperFillManager = Java.type(" | ||
| + | var JasperExportManager = Java.type(" | ||
| + | |||
| + | compiledReport=" | ||
| + | outputName = " | ||
| + | |||
| + | try { | ||
| + | reportStream = new FileInputStream(compiledReport); | ||
| + | parameters = new HashMap(); //empty hashmap | ||
| + | jasperPrint = JasperFillManager.fillReport( reportStream , parameters); | ||
| + | JasperExportManager.exportReportToPdfFile(jasperPrint, | ||
| + | |||
| + | | ||
| + | } catch (e) { | ||
| + | print(" | ||
| + | print(e); | ||
| + | | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | Go to the Test Frame of MIStudio and press the " | ||
| + | |||
| + | <note tip>If you see the error: | ||
| + | net.sf.jasperreports.engine.JRRuntimeException: | ||
| + | |||
| + | That's the end of this basic Tutorial. You can also generate other report types from this same SimpleReport.jasper file (see code examples below). | ||
| + | |||
| + | ===Generate a Spreadsheet from SimpleReport=== | ||
| + | |||
| + | Instead of generating a PDF you can make an xls or xlsx report (excel spreadsheets), | ||
| + | |||
| + | To make an xls spreadsheet, | ||
| + | |||
| + | <code javascript> | ||
| + | //Script to run SimpleReport.jasper as an XLS Excel Spreadsheet | ||
| + | |||
| + | var HashMap = Java.type(" | ||
| + | var FileInputStream = Java.type(" | ||
| + | var JasperPrint = Java.type(" | ||
| + | var JasperFillManager = Java.type(" | ||
| + | var JRXlsExporter = Java.type(" | ||
| + | var SimpleOutputStreamExporterOutput = Java.type(" | ||
| + | var SimpleExporterInput = Java.type(" | ||
| + | |||
| + | compiledReport=" | ||
| + | outputName = " | ||
| + | |||
| + | try { | ||
| + | reportStream = new FileInputStream(compiledReport); | ||
| + | parameters = new HashMap(); //empty hashmap | ||
| + | jasperPrint = JasperFillManager.fillReport( reportStream , parameters); | ||
| + | xlsExporter = new JRXlsExporter(); | ||
| + | xlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint)); | ||
| + | xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputName)); | ||
| + | xlsExporter.exportReport(); | ||
| + | |||
| + | | ||
| + | } catch (e) { | ||
| + | print(" | ||
| + | print(e); | ||
| + | | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | to make an xlsx spreadsheet, | ||
| + | |||
| + | <code javascript> | ||
| + | //Script to run SimpleReport.jasper as an XLSX Excel Spreadsheet | ||
| + | |||
| + | var HashMap = Java.type(" | ||
| + | var FileInputStream = Java.type(" | ||
| + | var JasperPrint = Java.type(" | ||
| + | var JasperFillManager = Java.type(" | ||
| + | var JRXlsxExporter = Java.type(" | ||
| + | var SimpleOutputStreamExporterOutput = Java.type(" | ||
| + | var SimpleExporterInput = Java.type(" | ||
| + | |||
| + | compiledReport=" | ||
| + | outputName = " | ||
| + | |||
| + | try { | ||
| + | reportStream = new FileInputStream(compiledReport); | ||
| + | parameters = new HashMap(); //empty hashmap | ||
| + | jasperPrint = JasperFillManager.fillReport( reportStream , parameters); | ||
| + | xlxsExporter = new JRXlsxExporter(); | ||
| + | xlxsExporter.setExporterInput(new SimpleExporterInput(jasperPrint)); | ||
| + | xlxsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputName)); | ||
| + | xlxsExporter.exportReport(); | ||
| + | |||
| + | | ||
| + | } catch (e) { | ||
| + | print(" | ||
| + | print(e); | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | to make a csv file, use this script: | ||
| + | |||
| + | <code javascript> | ||
| + | //Script to run SimpleReport.jasper to generate a CSV file | ||
| + | |||
| + | var HashMap = Java.type(" | ||
| + | var FileInputStream = Java.type(" | ||
| + | var JasperPrint = Java.type(" | ||
| + | var JasperFillManager = Java.type(" | ||
| + | var JRCsvExporter = Java.type(" | ||
| + | var SimpleCsvExporterConfiguration = Java.type(" | ||
| + | var SimpleWriterExporterOutput = Java.type(" | ||
| + | var SimpleExporterInput = Java.type(" | ||
| + | |||
| + | compiledReport=" | ||
| + | outputName = " | ||
| + | |||
| + | try { | ||
| + | reportStream = new FileInputStream(compiledReport); | ||
| + | parameters = new HashMap(); //empty hashmap | ||
| + | parameters.put(" | ||
| + | jasperPrint = JasperFillManager.fillReport( reportStream , parameters); | ||
| + | csvExporter = new JRCsvExporter(); | ||
| + | csvExporter.setExporterInput(new SimpleExporterInput(jasperPrint)); | ||
| + | csvExporter.setExporterOutput(new SimpleWriterExporterOutput(outputName)); | ||
| + | csvConfiguration = new SimpleCsvExporterConfiguration(); | ||
| + | csvConfiguration.setWriteBOM(true); | ||
| + | csvConfiguration.setRecordDelimiter(" | ||
| + | csvExporter.setConfiguration(csvConfiguration); | ||
| + | csvExporter.exportReport(); | ||
| + | | ||
| + | } catch (e) { | ||
| + | print(" | ||
| + | print(e); | ||
| + | | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ===Run the SimpleReport from TransSECS=== | ||
| + | |||
| + | You can trigger a script in TransSECS Devices with a CEID (Collection Event) or any other trigger to run the same reports as you can run in MIStudio and MIX. Copy the files listed in the setup section to your TransSECS project' | ||
| + | |||
| + | |||
gettingstartedwithjasperreports.1641417381.txt.gz · Last modified: by wikiadmin
