Writing code to drive a report of type JasperReport

A report-driver program is an ordinary EGL program that runs a report using a .jasper file that is compiled from a report design file.
The following example shows a report driver program that uses data from a specified database connection:
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.

The following table explains the code in the previous example:
Code Explanation
SQLLib.defineDatabaseAlias("customer",
  "jdbc:derby:C:\\databases
    \\CustomerDatabase");
SQLLib.connect("customer",
  "admin", "admin");
Connects to a database and defines an alias for the database using functions in the EGL library SQLLib
myReport Report = new Report();
Creates a new report variable to represent the report
myReportData ReportData = 
  new ReportData();
Creates a new report data variable to represent the data in the report
myReport.reportDesignFile = 
  location;
Sets the location of the .jasper file that is compiled from the report design file
myReport.reportDestinationFile = 
  location;
Sets the location of a temporary file that is used while processing the report
myReport.reportExportFile = 
  location;
Sets the location of the exported report
myReportData.connectionName = 
"customer";
Sets the database connection that the report uses, in terms of the database alias defined earlier
myReportData.sqlStatement = 
  SQL statement;
Sets the database access statement that provides the data for the report
myReport.reportData = myReportData;
Associates the report variable with the report data variable
reportLib.fillReport(myReport, 
  DataSource.sqlStatement);
Fills the report with data
reportLib.exportReport(myReport, 
  ExportFormat.pdf);
Runs and exports the report

Populating a report from a customized record

The following code snippet shows how you might fill a report using your own internal record as the data source. First, define a record like the following example, somewhere outside the program:
Record myRecord type BasicRecord
    author String;
    description String;
    title String;
end
Next, write the function to fill and drive the report:
//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

Selecting an export format

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.

The reportLib.exportReport() function recognizes the following parameters:
  • ExportFormat.html
  • ExportFormat.pdf
  • ExportFormat.text
  • ExportFormat.xml
  • ExportFormat.csv
For example, the following code causes JasperReports to export a report as a PDF file:
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.


Feedback