在 DynamicJasper 中使用子报表
我一直在用 Grails 开发一个项目。我需要生成 pdf、excel 和 word 格式的报告。我也能够以上述格式生成报告。我使用 DynamicJasper 生成报告。
现在我需要生成的报告中的子报告。
生成的报告包含下表和饼图,现在我需要一个子报告,如下所示,在同一个表 1 中 :
表 1
SN | Top 20 |Reporting |Comparison
1 | Dorsopathies |$181 |$5
2 | Special |$23 |$6
3 | Other Wounds |$20 |$0
报告期
Members| M Cost |P Cost
47 | $4,995,541 |$1,598,049
234 | $2,576,497 |$824,209
我生成报告的代码如下:
if (params.format) {
String svg = params.svg
def imagePath = fetchService.getChartImage(svg)
FastReportBuilder drb = new FastReportBuilder();
if (imagePath && imagePath!='') {
drb.setTemplateFile(root+"/reports/template_chart.jrxml")
} else {
drb.setTemplateFile(root+"/reports/template.jrxml")
}
Style changeStyle = new Style()
changeStyle.setHorizontalAlign(HorizontalAlign.CENTER)
DynamicReport dr = drb.addColumn("S.N", "S.N",Integer.class.getName(),30, changeStyle).
addColumn("Top 20", "Top 20 ",String.class.getName(),150).
addColumn("Reporting ","Reporting ",Double.class.getName(), 50, false, '$0.00').
addColumn("Comparison ","Comparison ",Double.class.getName(), 50, false, '$0.00').
addColumn("Prior Period Ranking","Prior Period Ranking",String.class.getName(), 50, changeStyle).
setTitle(" ").
setUseFullPageWidth(true).
build();
JRDataSource ds = new JRBeanCollectionDataSource(data);
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr,new ClassicLayoutManager(), ds);
HashMap reportParam = new HashMap();
reportParam.put("TITLE","Top 20 ");
reportParam.put("IMAGE_LINK",root+"/images/report_logo.jpg");
reportParam.put("CHART_IMAGE",imagePath);
generateReport(params.format, jp)
fetchService.deleteTempFile(imagePath)
return
}
[data: data, columns: columns , totalRep : totalRep, totalCom:totalCom, currentPage:'']
}
def generateReport(String format, JasperPrint jp){
ReportWriter reportWriter
switch (format) {
case "word":
JRDocxExporter docExporter = new JRDocxExporter();
ByteArrayOutputStream docStream = new ByteArrayOutputStream();
docExporter.setParameter(JRDocxExporterParameter.JASPER_PRINT,jp);
docExporter.setParameter(JRDocxExporterParameter.OUTPUT_STREAM,docStream);
docExporter.setParameter(JRDocxExporterParameter.FLEXIBLE_ROW_HEIGHT, Boolean.TRUE);
docExporter.exportReport();
def docResume = docStream.toByteArray();
response.setHeader("Content-disposition", "attachment; filename=\"" + "Report" + ".docx\"");
response.outputStream << docResume
case "excel":
reportWriter = ReportWriterFactory.getInstance().getReportWriter(jp, 'XLS', [:]);
response.setHeader("Content-disposition", "attachment; filename=\"" + "Report" + ".xls\"");
break
default:
reportWriter = ReportWriterFactory.getInstance().getReportWriter(jp, 'PDF', [:]);
response.setHeader("Content-disposition", "attachment; filename=\"" + "Report" + ".pdf\"");
break
}
reportWriter?.writeTo(response);
return
}
I have been working on a project in Grails. I need to generate reports in pdf, excel, and word. I have been able to generate reports also in the above mentioned formats. I have used DynamicJasper to generate the reports.
Now I need sub reports in the generated report.
The generated reports contain the following table along with a pie chart, and now I need a sub report like shown below in this same table no 1
:
Table no 1
SN | Top 20 |Reporting |Comparison
1 | Dorsopathies |$181 |$5
2 | Special |$23 |$6
3 | Other Wounds |$20 |$0
Reporting Period
Members| M Cost |P Cost
47 | $4,995,541 |$1,598,049
234 | $2,576,497 |$824,209
My code to generate the report is like this:
if (params.format) {
String svg = params.svg
def imagePath = fetchService.getChartImage(svg)
FastReportBuilder drb = new FastReportBuilder();
if (imagePath && imagePath!='') {
drb.setTemplateFile(root+"/reports/template_chart.jrxml")
} else {
drb.setTemplateFile(root+"/reports/template.jrxml")
}
Style changeStyle = new Style()
changeStyle.setHorizontalAlign(HorizontalAlign.CENTER)
DynamicReport dr = drb.addColumn("S.N", "S.N",Integer.class.getName(),30, changeStyle).
addColumn("Top 20", "Top 20 ",String.class.getName(),150).
addColumn("Reporting ","Reporting ",Double.class.getName(), 50, false, '$0.00').
addColumn("Comparison ","Comparison ",Double.class.getName(), 50, false, '$0.00').
addColumn("Prior Period Ranking","Prior Period Ranking",String.class.getName(), 50, changeStyle).
setTitle(" ").
setUseFullPageWidth(true).
build();
JRDataSource ds = new JRBeanCollectionDataSource(data);
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr,new ClassicLayoutManager(), ds);
HashMap reportParam = new HashMap();
reportParam.put("TITLE","Top 20 ");
reportParam.put("IMAGE_LINK",root+"/images/report_logo.jpg");
reportParam.put("CHART_IMAGE",imagePath);
generateReport(params.format, jp)
fetchService.deleteTempFile(imagePath)
return
}
[data: data, columns: columns , totalRep : totalRep, totalCom:totalCom, currentPage:'']
}
def generateReport(String format, JasperPrint jp){
ReportWriter reportWriter
switch (format) {
case "word":
JRDocxExporter docExporter = new JRDocxExporter();
ByteArrayOutputStream docStream = new ByteArrayOutputStream();
docExporter.setParameter(JRDocxExporterParameter.JASPER_PRINT,jp);
docExporter.setParameter(JRDocxExporterParameter.OUTPUT_STREAM,docStream);
docExporter.setParameter(JRDocxExporterParameter.FLEXIBLE_ROW_HEIGHT, Boolean.TRUE);
docExporter.exportReport();
def docResume = docStream.toByteArray();
response.setHeader("Content-disposition", "attachment; filename=\"" + "Report" + ".docx\"");
response.outputStream << docResume
case "excel":
reportWriter = ReportWriterFactory.getInstance().getReportWriter(jp, 'XLS', [:]);
response.setHeader("Content-disposition", "attachment; filename=\"" + "Report" + ".xls\"");
break
default:
reportWriter = ReportWriterFactory.getInstance().getReportWriter(jp, 'PDF', [:]);
response.setHeader("Content-disposition", "attachment; filename=\"" + "Report" + ".pdf\"");
break
}
reportWriter?.writeTo(response);
return
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以从
JasperReportBuilder
创建一个类型对象来设置报表的属性:使用
SubreportBuilder
创建一个对象:打印您的子报表:
You can create a type object from
JasperReportBuilder
to set Properties to report:With
SubreportBuilder
create an Object:Print your subreport: