1. You have two ways to use Jasper reports, one is to download and installe the Jaser Server and use it. It'll install Tomcat and by default PostgreSql, and an web application for managing your reports. The other way is to include Jasper reports libs in your application and manage the report generation and export programmatically by yourself.
2. Jasper report generation include these steps: create template (.jrxml), compile template (.jasper), fill report template (.jrprint), and then save the report to disk/ or export to other format/media, such as web application pages.
3. Jasper repot template normally generated with a tool, such as iReports.
But the template generated by IReport normally has the SQL query embedded inside the xml template. You can remove the query section and recomile it to get the .jasper file. You can create your own data source and fill the report accordingly, like by a JRResultSetDataSource. This way, you move the DAO to your own program and control it programmtically.
4. To fill a comipled Jasper report template:
JasperFillManager.fillReportToFile(String sourceFile, String destFile, Map params, JRDataSource ds);
5. To export a Jasper report to PDF as one go: (it can be jdbc or JPA)
String sql = "ur query";
Connection conn = DAOUtil.getConnection(dsname);
ResultSet resultSet = ....
JRResultSetDataSource ds = new JRResultSetDataSource(resultSet);
JasperRunManager.runReportToPdsStream(rptAsInStream, outputStream, new HashMap(), ds);
statement.close();
resultSet.close();
conn.close();
// if jsf, you wanna break its life cycle
FacesContext fc = FacesContext.getCurrentInstance();
fc.responseComplete();
// if http, you wanna set header(s) here
// response.setContenType("application/pdf");
outputStream.flush();
outputStream.close();
6. The normal steps to export to a pdf file:
File file = new File("/report/myReport.jrprint");
try {
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(file);
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
pdfExporter.setParameter(JRExporterOUTPUT_FILE_NAME, "/export/myReport.pdf");
pdfExporter.exportReport();
}
catch(Exception e) {
e.printStacktrace();
}
7. It's similar to export to Excel file:
File file = new File("/report/myReport.jrprint");
try {
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(file);
JExcelApiExporter xlsExporter = new JExcelApiExporter ();
xlsExporter .setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
xlsExporter .setParameter(JRExporterOUTPUT_FILE_NAME, "/export/myReport.xls");
xlsExporter .exportReport();
}
catch(Exception e) {
e.printStacktrace();
}
Note: it's preferable to use JExcelApiExporter, which is the newer implementation of Excel exporter. The original JrXlsExporter is does not support exporting images.
That's all for an introduction to Jasper report (-;
本文介绍了JasperReports的两种使用方式:通过安装JasperServer进行报告管理和直接在应用程序中集成JasperReports库来生成报告。文章详细阐述了报告生成过程,包括创建模板、填充报告及导出为PDF或Excel等格式。
1417

被折叠的 条评论
为什么被折叠?



