Wednesday, September 14, 2011

Dynamic Column in JasperReports

If you are looking for a dynamic column for your JasperReports, this is the right place. In this post, I will show you how to generate a dynamic column report with a JasperReports template file.

This is a basic dynamic column report that help you to kick start.

We are using DynamicJasper (3.0.6), JasperReports (2.0.5) and iReport (2.0.5) for this post. I know the version is out of date but the steps should be quite similar.

Create a JasperReports template for your report by using iReport.


You can see the columnHeader band is empty, without columns. We will use DynamicJasper to populate the columns into that band.

The following is the sample java code to generate dynamic columns report.

public class DynamicJasperTemplate {

protected JasperPrint jp;

protected JasperReport jr;

protected Map params = new HashMap();

protected DynamicReport dr;

public void buildReport() throws Exception {

params.put("ReportTitle1", "Report Title");

DynamicReportBuilder drb = new DynamicReportBuilder();

Font font = new Font(10,"Sarif",true);

Style headerStyle = new Style();

headerStyle.setFont(font);

headerStyle.setHorizontalAlign(HorizontalAlign.LEFT);

headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);

font = new Font(8,"Sarif",false);

Style detailStyle = new Style();

detailStyle.setFont(font);

detailStyle.setHorizontalAlign(HorizontalAlign.LEFT);

detailStyle.setVerticalAlign(VerticalAlign.MIDDLE);

for(int i=1; i<=10; i++) {

AbstractColumn column = ColumnBuilder.getInstance()

.setColumnProperty("Column"+i, String.class.getName())

.setTitle("Column "+i+" Title").setWidth(200)

.setStyle(detailStyle).setHeaderStyle(headerStyle).build();

drb.addColumn(column);

}

drb.setUseFullPageWidth(true);

// Pass the JasperReport Template to DynamicJasper

drb.setTemplateFile("C:/DynamicJasperTemplate.jrxml");

DynamicReport dr = drb.build();

JRDataSource ds = getDataSource();

jr = DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager(), params);

if (ds != null) {

jp = JasperFillManager.fillReport(jr, params, ds);

} else {

jp = JasperFillManager.fillReport(jr, params);

}

JasperExportManager.exportReportToPdfFile(jp,"C:/report-out.pdf");

}

protected JRDataSource getDataSource() {

// Generate dummy data to show in the report.

List records = new ArrayList();

for(int i=1; i<10; i++) {

Map columns = new HashMap();

for (int j=1; j<=10; j++) {

// The HashMap Key must save with ColumnProperty Name

columns.put("Column"+j, "Record "+i+" Column "+j+" data.");

}

records.add(columns);

}

JRDataSource ds = new JRMapCollectionDataSource(records);

return ds;

}

public static void main(String[] args) throws Exception {

DynamicJasperTemplate djt = new DynamicJasperTemplate();

djt.buildReport();

}

}



This is the output PDF file.

2 comments:

Ayşegül Yazıcı said...
This comment has been removed by the author.
odyllvalencic said...

Harrah's Cherokee Casino Resort - JamBase
Harrah's Cherokee Casino 부산광역 출장안마 Resort locations, rates, amenities: expert Cherokee research, only at Hotel and Travel Index. 포항 출장마사지 Rating: 4.6 수원 출장마사지 · ‎919 경상남도 출장마사지 votes 서산 출장안마