Friday, March 6, 2015

DynamicReports: How to save a report for viewing later

Step 1. Create and Save the report

public class DynamicReportsTest {
       public DynamicReportsTest() {
            JasperReportBuilder report = DynamicReports.report();

            TextColumnBuilder<Float> priceColumn = Columns.column("Price", "price", DataTypes.floatType());
             TextColumnBuilder<Integer> quantityOrderedColumn = Columns.column("Quantity Ordered", "quantityOrdered", DataTypes.integerType());
             TextColumnBuilder<BigDecimal> totalPay = priceColumn.multiply(quantityOrderedColumn)
                .setTitle("Amount Paid"));

            report.columns(Columns.columnRowNumberColumn("Item"),
                Columns.column("Name", "name", DataTypes.stringType()),
                priceColumn,
                quantityOrderedColumn,
                totalPay);
             report.title(Components.text("Test Report").setStyle(stl.style().blod());

            report.setDataSource(createDataSource());
            try {
                   //Save the report to a file.
                  JRSaver.saveObject(report.toJasperPrint(),  "<file path and name>");
                   report.show();
            }
      }

       private JRDataSource createDataSource() {
              DRDataSource dataSource = new DRDataSource("name", "price", "quantityOrdered");
              dataSource.add("Apple", new BigDecimal(1.29), 120);
              dataSource.add("Apple", new BigDecimal(1.69), 150);
              dataSource.add("Orange", new BigDecimal(0.99), 130);
              dataSource.add("Orange", new BigDecimal(0.96), 100);
             dataSource.add("Mange", new BigDecimal(0), 300);
             return dataSource;
      }
 
      public static void main(String[] args){
           new DynamicReportsTest();
      }
}


Step 2. View the saved report

public class viewSavedReport {
      public static void main(String[] args) {
              //View the saved report in step 1
             JasperPrint rpt = (JasperPrint)JRLoader.loadObject(new java.io.FileInputStream("<file path and name>"));
              reportViewer = new JasperViewer(rpt, false);
             reportViewer.setTitle("<Your report viewer title>");
             reportViewer.setZoomRatio(new Float(0.8949)); //the frame fit ratio
             reportViewer.setVisible(true);
      }
}
       
-----------------------------------------------------------------------------------------------------------------------
                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

3 comments:

  1. Hi,

    Thanks for writing such informative JasperReport blogs. I saw your past blog on jasperreport.
    I need your help in one problem in one problem, that question was posted at

    http://stackoverflow.com/questions/31251207/dynamicreport-how-to-change-column-data-values-to-use-user-friendly-values


    Many thanks for your help

    ReplyDelete
  2. It seems that you are using a query to set up your data source: report.setDataSource(Query, connection);

    You may modify you query to return the String instead of the number for the employee attendance.

    SELECT CASE EMPLOYEE.ATTENDANCE WHEN 1 THEN 'Present' WHEN 2 THEN 'Absent' WHEN 3 THEN 'Leave**' ELSE ' ' END AS ATTENDANCE FROM EMPLOYEE

    ReplyDelete