Jump to content
We've recently updated our Privacy Statement, available here ×

i want to generate subreport in Ireport3.6


anupp

Recommended Posts

Hi, i want to genearte sub report in Ireport  without datasource/Query exceuting,Actually i m getting some problem.

I have passed data thorogh hashMap.so in a Report data is generated and  first report data i m getting antother sub report data i m also getting but data is comiming NULL.So where i m getting missing plz reponse me .or reply me.

 

Thanks in Advance

Code:
package demo.view;import java.io.InputStream;import java.util.HashMap;import java.util.Map;import net.sf.jasperreports.engine.JRDataSource;import net.sf.jasperreports.engine.JREmptyDataSource;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JasperCompileManager;import net.sf.jasperreports.engine.JasperExportManager;import net.sf.jasperreports.engine.JasperPrint;import net.sf.jasperreports.engine.JasperReport;import net.sf.jasperreports.engine.JasperRunManager;import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;import net.sf.jasperreports.engine.data.JRMapArrayDataSource;import net.sf.jasperreports.engine.fill.JRFiller;import net.sf.jasperreports.view.JasperViewer;public class Test3 {    public Test3() {        super();    }    public static void main(String[] args) {           String reportSource = "c:/reportApp.jrxml";        String reportDest = "c:/agile12.pdf";        try {            JasperReport jasperReport =                JasperCompileManager.compileReport(reportSource);            JRDataSource dataSource = createReportDataSource();            JasperPrint jasperPrint =                JRFiller.fillReport(jasperReport, new HashMap(), dataSource);            JasperExportManager.exportReportToPdfFile(jasperPrint, reportDest);            JasperViewer.viewReport(jasperPrint);        }        catch (JRException ex) {            ex.printStackTrace();        }    }    private static JRDataSource createReportDataSource() {        JRMapArrayDataSource dataSource;        Map[] reportRows = initializeMapArray();        dataSource = new JRMapArrayDataSource(reportRows);        return dataSource;    }    private static Map[] initializeMapArray() {        HashMap[] reportRow = new HashMap[5];        HashMap<String, Object> Qty = new HashMap<String, Object>();        HashMap<String, Object> Certi = new HashMap<String, Object>();        HashMap<String, Object> address = new HashMap<String, Object>();        HashMap<String, Object> contact = new HashMap<String, Object>();                HashMap<String,  HashMap<String, Object>> address2 = new HashMap<String,  HashMap<String, Object>>();                Qty.put("certificateNo", "xyx");        Qty.put("quantity", 101);        Certi.put("certificateNo", "HealthIns");        Certi.put("quantity", 1255);                    address.put("address","patna");        address.put("phone",54488);                address2.put("address2", address);                    contact.put("address","mumbai");      contact.put("phone",7841);                reportRow[0] =  Qty ;        reportRow[1] = Certi;                reportRow[2] = address;        reportRow[3] = contact;        reportRow[4] = address2;        return reportRow;    }}
Link to comment
Share on other sites

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

anupp, I think I did not get very much what you are trying to do, but here are some hints.

1. a JRMapArrayDataSource datasource is composed by a set of maps, one for each record. Each map entry is something like ("colum name", "column value"). Each entry of the array is a record of the report:

array[0] = { "name"=>"Giulio", "adress"=>"AAA", "country"=>"Italy" }
array[1] = { "name"=>"John", "adress"=>"BBB", "country"=>"USA" }
array[2] = { "name"=>"Larry", "adress"=>"CCC", "country"=>"USA" }
array[3] = { "name"=>"Mandy", "adress"=>"FFF", "country"=>"France" }

(this is just metacode to explain the content of each map and the array, this syntax to populate a map does not work in java)

2. A subreport needs a datasource to be filled. If you use a JREmptyDatasource() you'll get a subreport with a single record with all the fields set to null.

Since I suppose you need to print in the subreport some data relevant to a particular record (i.e. the hobbies of the person), you need to create a datasource for that.

You can set the hobbies of each person by creating for each of them an array of maps and add this array to the record in a field called for instance "hobbies". Here is the code:

Map[] hobbies = new HashMap[2];

hobbies[0]  = new HashMap() {  "hobby"=>"Photography"}   <- The map that holds the record of the sub-datasource
hobbies[1]  = new HashMap() {  "hobby"=>"Travels"}

and then...

array[0].put("hobbies", hobbies);    <-- Here I'm adding the field hobbies, the value of the field is the hobbies array of maps.

The datasource expression for the subreport will be:

new JRMapArrayDataSource(  (Map[])$F{hobbies} )

Hope this can help

Giulio

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...