The subreport function allows multiple reports to be included into one report one after another.
public class SubreportTest {
private StyleBuilder bold;
private StyleBuilder centeredBold;
private StyleBuilder columnTitleStyle;
private StyleBuilder columnStyle;
private JasperReportBuilder reportBuilder;
public SubreportTest() {
bold = Styles.style().bold();
centeredBold = Styles.style(bold)
.setHorizontalAlignment(HorizontalAlignment.CENTER);
columnTitleStyle = Styles.style(centeredBold)
.setBackgroundColor(Color.LIGHT_GRAY)
.setBorder(Styles.pen1Point());
columnStyle = Styles.style()
.setHorizontalAlignment(HorizontalAlignment.LEFT)
.setLeftIndent(4)
.setBottomPadding(5);
build();
}
public void build() {
//Creating the subreport
SubreportExpression subreportExpress = new SubreportExpression();
SubreportBuilder subreport = Components.subreport(subreportExpress);
subreport.setDataSource(new SubreportDataSourceExpression());
reportBuilder = DynamicReports.report();
//Formatting the subreports
//Using default page break and setting the gap between subreports to 20
reportBuilder.detail(subreport, Components.verticalGap(20));
//Beginning each subreport on a new page
//reportBuilder.detail(subreport, Components.pageBreak());
//Only having a page break at a subreport
//reportBuilder.detail(subreport, Components.pageBreak().setPrintWhenExpression(new PageBreakExpression()));
//No page break
//reportBuilder.ignorePagination();
//Setting the page header (the title to be displayed on top of each page)
reportBuilder.pageHeader(Components.text("Comprehensive Report").setStyle(centeredBold));
reportBuilder.pageHeader(Components.text("Statistics of Market Development").setStyle(centeredBold));
reportBuilder.pageHeader(Components.horizontalList(Components.text("Date: " + new Date().toString()),
Components.filler().setFixedWidth(420),
Components.text("Pay Attention Please")));
//Setting the page footer
reportBuilder.pageFooter(Components.horizontalFlowList().add(Components.text("Page "))
.add(Components.pageNumber())
.setStyle(bold));
//Setting the number of subreports
reportBuilder.setDataSource(new JREmptyDataSource(2));
reportBuilder.show();
reportBuilder.toPdf(new FileOutputStream(new File(<fileName>)));
}
private class SubreportExpression extends AbstractSimpleExpression<JasperReportBuilder> {
public JasperReportBuilder evaluate(ReportParameters reportParameters) {
int masterRowNumber = reportParameters.getReportRowNumber();
JasperReportBuilder report = DynamicReports.report();
//DynamicReports subreport row starts from 1 instead of 0
if (masterRowNumber == 1){
report.columns(Columns.column("Col 1", "col1Data", DataTypes.stringType()),
Columns.column("Col 2", "col2Data", DataTypes.stringType()));
report.setColumnTitleStyle(columnTitleStyle.setBottomBorder(Styles.penDashed()));
report.setColumnStyle(columnStyle);
report.highlightDetailEvenRows();
} else if (masterRowNumber == 2){
report.columns(Columns.column("Column 1", "col3Data", DataTypes.stringType()),
Columns.column("Column 2", "col4Data", DataTypes.stringType()));
report.setColumnTitleStyle(columnTitleStyle.setBottomBorder(Styles.penDashed()));
report.setColumnStyle(columnStyle);
report.highlightDetailEvenRows();
}
}
return report;
}
private class SubreportDataSourceExpression extends AbstractSimpleExpression<JRDataSource> {
//private ArrayList<DRDataSource> dataSourceArray;
//public SubreportDataSourceExpression(ArrayList<DRDataSource> dataSourceArray){
// super();
// this.dataSourceArray = dataSourceArray;
//}
public JRDataSource evaluate(ReportParameters reportParameters) {
int masterRowNumber = reportParameters.getReportRowNumber();
DRDataSource dataSource;
if (masterRowNumber == 1) {
dataSource = new DRDataSource("col1Data", "col2Data");
dataSource.add("Apple", "Sweet Fruit");
dataSource.add("Lemon", "Sour Fruit");
} else if (masterRowNumber == 2) {
dataSource = new DRDataSource("col3Data", "col4Data");
dataSource.add("Diamond", "Transparent");
dataSource.add("Ruby", "Red");
}
return dataSource;
// return dataSourceArray.get(masterRowNumber-1);
}
}
private class PageBreakExpression extends AbstractSimpleExpression<Boolean> {
public Boolean evaluate(ReportParameters reportParameters) {
//Only having a page break after the first subreport
if (reportParameters.getReportRowNumber() == 1) {
return true;
} else {
return false;
}
}
}
}
Previous<
---------------------------------------------------------------------------------------------------------------
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.
public class SubreportTest {
private StyleBuilder bold;
private StyleBuilder centeredBold;
private StyleBuilder columnTitleStyle;
private StyleBuilder columnStyle;
private JasperReportBuilder reportBuilder;
public SubreportTest() {
bold = Styles.style().bold();
centeredBold = Styles.style(bold)
.setHorizontalAlignment(HorizontalAlignment.CENTER);
columnTitleStyle = Styles.style(centeredBold)
.setBackgroundColor(Color.LIGHT_GRAY)
.setBorder(Styles.pen1Point());
columnStyle = Styles.style()
.setHorizontalAlignment(HorizontalAlignment.LEFT)
.setLeftIndent(4)
.setBottomPadding(5);
build();
}
public void build() {
//Creating the subreport
SubreportExpression subreportExpress = new SubreportExpression();
SubreportBuilder subreport = Components.subreport(subreportExpress);
subreport.setDataSource(new SubreportDataSourceExpression());
reportBuilder = DynamicReports.report();
//Formatting the subreports
//Using default page break and setting the gap between subreports to 20
reportBuilder.detail(subreport, Components.verticalGap(20));
//Beginning each subreport on a new page
//reportBuilder.detail(subreport, Components.pageBreak());
//Only having a page break at a subreport
//reportBuilder.detail(subreport, Components.pageBreak().setPrintWhenExpression(new PageBreakExpression()));
//No page break
//reportBuilder.ignorePagination();
//Setting the page header (the title to be displayed on top of each page)
reportBuilder.pageHeader(Components.text("Comprehensive Report").setStyle(centeredBold));
reportBuilder.pageHeader(Components.text("Statistics of Market Development").setStyle(centeredBold));
reportBuilder.pageHeader(Components.horizontalList(Components.text("Date: " + new Date().toString()),
Components.filler().setFixedWidth(420),
Components.text("Pay Attention Please")));
//Setting the page footer
reportBuilder.pageFooter(Components.horizontalFlowList().add(Components.text("Page "))
.add(Components.pageNumber())
.setStyle(bold));
//Setting the number of subreports
reportBuilder.setDataSource(new JREmptyDataSource(2));
reportBuilder.show();
reportBuilder.toPdf(new FileOutputStream(new File(<fileName>)));
}
private class SubreportExpression extends AbstractSimpleExpression<JasperReportBuilder> {
public JasperReportBuilder evaluate(ReportParameters reportParameters) {
int masterRowNumber = reportParameters.getReportRowNumber();
JasperReportBuilder report = DynamicReports.report();
//DynamicReports subreport row starts from 1 instead of 0
if (masterRowNumber == 1){
report.columns(Columns.column("Col 1", "col1Data", DataTypes.stringType()),
Columns.column("Col 2", "col2Data", DataTypes.stringType()));
report.setColumnTitleStyle(columnTitleStyle.setBottomBorder(Styles.penDashed()));
report.setColumnStyle(columnStyle);
report.highlightDetailEvenRows();
} else if (masterRowNumber == 2){
report.columns(Columns.column("Column 1", "col3Data", DataTypes.stringType()),
Columns.column("Column 2", "col4Data", DataTypes.stringType()));
report.setColumnTitleStyle(columnTitleStyle.setBottomBorder(Styles.penDashed()));
report.setColumnStyle(columnStyle);
report.highlightDetailEvenRows();
}
}
return report;
}
private class SubreportDataSourceExpression extends AbstractSimpleExpression<JRDataSource> {
//private ArrayList<DRDataSource> dataSourceArray;
//public SubreportDataSourceExpression(ArrayList<DRDataSource> dataSourceArray){
// super();
// this.dataSourceArray = dataSourceArray;
//}
public JRDataSource evaluate(ReportParameters reportParameters) {
int masterRowNumber = reportParameters.getReportRowNumber();
DRDataSource dataSource;
if (masterRowNumber == 1) {
dataSource = new DRDataSource("col1Data", "col2Data");
dataSource.add("Apple", "Sweet Fruit");
dataSource.add("Lemon", "Sour Fruit");
} else if (masterRowNumber == 2) {
dataSource = new DRDataSource("col3Data", "col4Data");
dataSource.add("Diamond", "Transparent");
dataSource.add("Ruby", "Red");
}
return dataSource;
// return dataSourceArray.get(masterRowNumber-1);
}
}
private class PageBreakExpression extends AbstractSimpleExpression<Boolean> {
public Boolean evaluate(ReportParameters reportParameters) {
//Only having a page break after the first subreport
if (reportParameters.getReportRowNumber() == 1) {
return true;
} else {
return false;
}
}
}
}
Previous<
---------------------------------------------------------------------------------------------------------------
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.
References:
3. DynamicReports in java (3) - Concatenating reports
4. DynamicReports in java (4) - Setting/formatting the title
4. DynamicReports in java (4) - Setting/formatting the title
No comments:
Post a Comment