program reportDriverProgram type BasicProgram
function main()
SQLLib.defineDatabaseAlias("customer",
"jdbc:derby:C:\\databases\\CustomerDatabase");
SQLLib.connect("customer", "admin", "admin");
myReport Report = new Report();
myReportData ReportData = new ReportData();
myReport.reportDesignFile =
"C:\\Workspace\\MyProject\\bin\\reportModel.jasper";
myReport.reportDestinationFile = "C:\\temp.jrprint";
myReport.reportExportFile = "C:\\MyReport.pdf";
myReportData.connectionName = "customer";
myReportData.sqlStatement =
"select * from EGL.CUSTOMER order by CUSTOMER_ID desc";
myReport.reportData = myReportData;
reportLib.fillReport(myReport, DataSource.sqlStatement);
reportLib.exportReport(myReport, ExportFormat.pdf);
end
end
Because the backslash character (\) is used in escape sequences, you must use a double backslash in path names.
| Code | Explanation |
|---|---|
|
Connects to a database and defines an alias for the database using functions in the EGL library SQLLib |
|
Creates a new report variable to represent the report |
|
Creates a new report data variable to represent the data in the report |
|
Sets the location of the .jasper file that is compiled from the report design file |
|
Sets the location of a temporary file that is used while processing the report |
|
Sets the location of the exported report |
|
Sets the database connection that the report uses, in terms of the database alias defined earlier |
|
Sets the database access statement that provides the data for the report |
|
Associates the report variable with the report data variable |
|
Fills the report with data |
|
Runs and exports the report |
Record myRecord type BasicRecord
author String;
description String;
title String;
end
//Variable declarations
myReport Report = new Report();
myReportData ReportData = new ReportData();
recArray myRecord[];
recArrayElement myRecord;
//Function containing the report driving code
function makeReport()
//Initialize report file locations
myReport.reportDesignFile =
"c:\\workspace\\report_project\\" ::
"bin\\report_package\\myReport.jasper";
myReport.reportDestinationFile =
"c:\\temp\\myReport.jrprint";
//Get the report data
populateReportData();
myReport.reportData = myReportData;
//Fill the report with data
reportLib.fillReport(myReport, DataSource.reportData);
//Export the report in HTML format
myReport.reportExportFile = "c:\\temp\\myReport.html";
reportLib.exportReport(myReport, ExportFormat.html);
end
function populateReportData()
recArrayElement.author="Jane Austen";
recArrayElement.title="Northanger Abbey";
recArrayElement.description = "British Novel";
recArray.appendElement(recArrayElement);
recArrayElement.author = "Jane Austen";
recArrayElement.title="Emma";
recArrayElement.description = "British Novel";
recArray.appendElement(recArrayElement);
recArrayElement.author = "Charles Dickens";
recArrayElement.title="Our Mutual Friend";
recArrayElement.description = "British Novel";
recArray.appendElement(recArrayElement);
recArrayElement.author = "Gustave Flaubert";
recArrayElement.title="Madame Bovary";
recArrayElement.description = "French Novel";
recArray.appendElement(recArrayElement);
recArrayElement.author = "M. Lermontov";
recArrayElement.title="Hero of Our Time";
recArrayElement.description = "Russian Novel";
recArray.appendElement(recArrayElement);
end
JasperReports saves filled report data as an intermediate destination file (extension .jrprint), from which it can create multiple export files. You can export filled reports as PDF, HTML, XML, CSV (comma-separated values that spreadsheet programs can read), and plain text output. To export the report, use the reportLib.exportReport() function in the EGL report-driver program with the report variable and a parameter that indicates the format of the report.
myReport.ReportExportFile = "c:\\temp\\my_report.pdf";
reportLib.exportReport(myReport, ExportFormat.pdf);
Important: JasperReports does not automatically refresh exported reports. If you change the report design or if data changes, you must refill and re-export the report.