Reputation: 29
I am having different columns so I want to display dynamic columns in one jasper report. But the main thing is columns are varying.
Is there any possibilities to write java code in jasper reports?
Please help me to overcome this problem.
Upvotes: 1
Views: 5192
Reputation: 455
There are options I used DynamicReports,
https://dynamicreports.lbayer.com/examples/examples-overview/
You can add columns at runtime.
private void printReport() throws DRException {
AdhocConfiguration configuration = new AdhocConfiguration();
AdhocReport report = new AdhocReport();
// column name list
List<String> columnList = new ArrayList<>();
columnList.add("Column 1");
columnList.add("Column 2");
columnList.add("Column 3");
columnList.add("Column ...");
columnList.add("Column n");
for (String column : columnList) {
AdhocColumn adhocColumn = new AdhocColumn();
adhocColumn.setTitle(column);
adhocColumn.setName(column);
report.addColumn(adhocColumn);
}
configuration.setReport(report);
String[] myArray = new String[columnList.size()];
DRDataSource dataSource = new DRDataSource(columnList.toArray(myArray));
dataSource.add("data1", "data2", "data3", "data ...", "data n");
dataSource.add("data1", "data2", "data3", "data ...", "data n");
JasperReportBuilder reportBuilder = AdhocManager.createReport(configuration.getReport());
reportBuilder.setDataSource(dataSource);
reportBuilder.show();
}
Upvotes: 0
Reputation: 26
You don't need to do it in java codes.
You can do it in jrxml file itself.
for example: You need to display 2 column in one jrxml file based on condition.
two fields are CASH RECIEPT, CARD RECIEPT.
Put them in almost same position in jrxml file where with site difference to ensure avoid errors (red marked error.)
first take CASH RECEIPT
right click on it and view the properties of it.
then find the Print When Expression(this is the place where you can apply the condition which should be true to print the column.) and go to it.
add your expression (condition.) its something like this $F{card}.equals( "0" ) ? true : false this must be changed according to your condition. HERE WHAT HAPPENS IS WHEN VALUE OF card field is zero in jrxml file it will print the CASH RECEIPT column
Do the same thing to CARD RECEIPT.
print when expression is as follow
$F{card}.equals( "1" ) ? true : false
What will do this is ,CARD RECEIPT will be Printed when card field is hold the value 1.
Upvotes: 0
Reputation: 4973
Yes.. you can use Dynamic Jasper for that. You can add dynamic columns and set all parameters for configuration whatever you want. It's easy and simple. Here I am giving some useful class names by which you can achieve the same.
Upvotes: 1