1.iReport-designer下载地址: http://community.jaspersoft.com/project/ireport-designer
2. 安装完成后,运行iReport,首先我们需要建立一个数据源,这里用MySql数据库做为报表的数据源
选择JDBC connection:
填写正确的数据库相关参数:
然后测试连接成功后,保存当前连接配置,完成后,连接列表中出现新的连接,并自动选择默认情况下,主动连接
3. 现在我们开始新建一个报表
选择“文件”->”新建”,出现模板选取器,选择一个空白的报表,然后单击打开这个模板
填写报表名称和位置,点击下一步,然后完成
完成之后显示工作界面
4.接下来我们需要关联数据源
按一下工具栏中的数据库查询设计器按钮,这将打开查询对话框
在查询对话框中,输入一条简单的SQL查询语句,选择所有字段/列。当然,你可以使用任何表,这个例子,我们使用的是样本数据库中的acc_user表。当你键入SQL后,iReport执行查询,提取所有字段,并在底部窗口中列出 。 如果在SQL中有一个错误,你会得到一个消息,说明什么是错的。
当查询是有效时,单击“确定”,可以发现数据源中所有的字段都自动添加到Fields中
5. 至此,我们就可以设计我们的报表了
从Fields中拖动字段到Detail域,要添加其他元素(如线条或标签),将它们从调色板拖动到设计视图,然后随意调整,并安排他们
若要预览报表在设计器中单击“预览”工具栏,切换到预览模式。 预览报告在后台编译,通过JDBC连接查询检索的数据填充它。
Detail域重复查询结果中的每一行,创建一个简单的报表
最近在使用Spring MVC +Ireport 3.7 完成统计报表,在这里做一些记录和分享。
1、Method(导出HTML,方法过时)
- @RequestMapping(value = “/exportHtml”)
- public void exportHtml(HttpServletRequest request,
- HttpServletResponse response) throws IOException, JRException {
- String ctxpath = request.getSession().getServletContext()
- .getRealPath(”/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper”);
- //得到要显示的数据
- List<Report> reportList = reportService.listAc01ApplicationReport();
- File reFile = new File(ctxpath);
- //这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
- Map parameters = new HashMap();
- parameters.put(’info’,‘哈哈哈’);
- //封装reportList中数据作为数据源
- JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
- JasperPrint jasperPrint = JasperFillManager.fillReport(
- reFile.getPath(), parameters, ds);
- //导出html(该方法)
- JRHtmlExporter exporter = new JRHtmlExporter();
- exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);
- exporter.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER, response.getWriter());
- exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
- exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, ”utf-8”);
- exporter.exportReport();
- }
@RequestMapping(value = "/exportHtml")
public void exportHtml(HttpServletRequest request,
HttpServletResponse response) throws IOException, JRException {
String ctxpath = request.getSession().getServletContext()
.getRealPath("/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper");
//得到要显示的数据
List<Report> reportList = reportService.listAc01ApplicationReport();
File reFile = new File(ctxpath);
//这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
Map parameters = new HashMap();
parameters.put('info','哈哈哈');
//封装reportList中数据作为数据源
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
JasperPrint jasperPrint = JasperFillManager.fillReport(
reFile.getPath(), parameters, ds);
//导出html(该方法)
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER, response.getWriter());
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "utf-8");
exporter.exportReport();
}
2、Method(导出EXCEL,方法过时)
- @RequestMapping(value = “/exportExcel”)
- public void exportExcel(HttpServletRequest request,
- HttpServletResponse response) throws IOException, JRException {
- String ctxpath = request.getSession().getServletContext()
- .getRealPath(”/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper”);
- //得到要显示的数据
- List<Report> reportList = reportService.listAc01ApplicationReport();
- File reFile = new File(ctxpath);
- //这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
- Map parameters = new HashMap();
- parameters.put(’info’,‘哈哈哈’);
- //封装reportList中数据作为数据源,irepot中通过获取对象(report)的属性获取其值
- JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
- JasperPrint jasperPrint = JasperFillManager.fillReport(
- reFile.getPath(), parameters, ds);
- //导出EXCEL
- JRXlsExporter exporter = new JRXlsExporter();
- exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
- exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream());
- exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
- exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
- response.setHeader(”Content-Disposition”, “attachment;filename=template.xls”);//设置导出文件名
- response.setContentType(”application/vnd_ms-excel;charset=utf-8”);
- exporter.exportReport();
- }
@RequestMapping(value = "/exportExcel")
public void exportExcel(HttpServletRequest request,
HttpServletResponse response) throws IOException, JRException {
String ctxpath = request.getSession().getServletContext()
.getRealPath("/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper");
//得到要显示的数据
List<Report> reportList = reportService.listAc01ApplicationReport();
File reFile = new File(ctxpath);
//这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
Map parameters = new HashMap();
parameters.put('info','哈哈哈');
//封装reportList中数据作为数据源,irepot中通过获取对象(report)的属性获取其值
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
JasperPrint jasperPrint = JasperFillManager.fillReport(
reFile.getPath(), parameters, ds);
//导出EXCEL
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
response.setHeader("Content-Disposition", "attachment;filename=template.xls");//设置导出文件名
response.setContentType("application/vnd_ms-excel;charset=utf-8");
exporter.exportReport();
}
3、Ireport模板
虽然以上的方法可以使用,但是在编译的时候提示方法过时,考虑到系统以后的扩展性,还是尽量不去使用过时方法,可以用以下方法来替代。
4、Method(导出HTML)
- @RequestMapping(value = “/exportHtml”)
- public void exportHtml(HttpServletRequest request,
- HttpServletResponse response) throws IOException, JRException {
- String ctxpath = request.getSession().getServletContext()
- .getRealPath(”/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper”);
- //得到要显示的数据
- List<Report> reportList = reportService.listAc01ApplicationReport();
- File reFile = new File(ctxpath);
- //这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
- Map parameters = new HashMap();
- parameters.put(’info’,‘哈哈哈’);
- //封装reportList中数据作为数据源
- JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
- JasperPrint jasperPrint = JasperFillManager.fillReport(
- reFile.getPath(), parameters, ds);
- //导出html,替换为这段
- HtmlExporter exporter=new HtmlExporter();
- exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
- exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getOutputStream()));
- SimpleHtmlExporterConfiguration configuration=new SimpleHtmlExporterConfiguration();
- exporter.setConfiguration(configuration);
- exporter.exportReport();
- }
@RequestMapping(value = "/exportHtml")
public void exportHtml(HttpServletRequest request,
HttpServletResponse response) throws IOException, JRException {
String ctxpath = request.getSession().getServletContext()
.getRealPath("/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper");
//得到要显示的数据
List<Report> reportList = reportService.listAc01ApplicationReport();
File reFile = new File(ctxpath);
//这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
Map parameters = new HashMap();
parameters.put('info','哈哈哈');
//封装reportList中数据作为数据源
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
JasperPrint jasperPrint = JasperFillManager.fillReport(
reFile.getPath(), parameters, ds);
//导出html,替换为这段
HtmlExporter exporter=new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getOutputStream()));
SimpleHtmlExporterConfiguration configuration=new SimpleHtmlExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
}
5、Method(导出EXCEL)
- @RequestMapping(value = “/exportHtml”)
- public void exportHtml(HttpServletRequest request,
- HttpServletResponse response) throws IOException, JRException {
- String ctxpath = request.getSession().getServletContext()
- .getRealPath(”/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper”);
- //得到要显示的数据
- List<Report> reportList = reportService.listAc01ApplicationReport();
- File reFile = new File(ctxpath);
- //这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
- Map parameters = new HashMap();
- parameters.put(’info’,‘哈哈哈’);
- //封装reportList中数据作为数据源
- JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
- JasperPrint jasperPrint = JasperFillManager.fillReport(
- reFile.getPath(), parameters, ds);
- //导出excel,替换为这段
- JRXlsExporter exporter = new JRXlsExporter();
- exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
- exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
- SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
- //设置导出文件名称
- response.setHeader(”Content-Disposition”, “attachment;filename=template.xls”);
- //设置导出文件格式
- response.setContentType(”application/vnd_ms-excel;charset=utf-8”);
- exporter.setConfiguration(configuration);
- exporter.exportReport();
- }
@RequestMapping(value = "/exportHtml")
public void exportHtml(HttpServletRequest request,
HttpServletResponse response) throws IOException, JRException {
String ctxpath = request.getSession().getServletContext()
.getRealPath("/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper");
//得到要显示的数据
List<Report> reportList = reportService.listAc01ApplicationReport();
File reFile = new File(ctxpath);
//这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
Map parameters = new HashMap();
parameters.put('info','哈哈哈');
//封装reportList中数据作为数据源
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
JasperPrint jasperPrint = JasperFillManager.fillReport(
reFile.getPath(), parameters, ds);
//导出excel,替换为这段
JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
//设置导出文件名称
response.setHeader("Content-Disposition", "attachment;filename=template.xls");
//设置导出文件格式
response.setContentType("application/vnd_ms-excel;charset=utf-8");
exporter.setConfiguration(configuration);
exporter.exportReport();
}
如果要导出其他格式的文件,只要改变导出适配器(Exporter)就可以了,例如要导出PDF,那就使用JRPdfExporter。很简单吧,但是纸上得来终觉浅,还是要亲自实践一下哦。
7、打印
- @RequestMapping(value = “/print”)
- public void print(HttpServletRequest request) throws IOException, JRException {
- String ctxpath = request.getSession().getServletContext()
- .getRealPath(”/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper”);
- //得到要显示的数据
- List<Report> reportList = reportService.listAc01ApplicationReport();
- File reFile = new File(ctxpath);
- //这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
- Map parameters = new HashMap();
- parameters.put(’info’,‘哈哈哈’);
- //封装reportList中数据作为数据源
- JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
- JasperPrint jasperPrint = JasperFillManager.fillReport(
- reFile.getPath(), parameters, ds);
- //替换掉导出的代码,true为弹出打印机设置,false为直接打印
- JasperPrintManager.printReport(jasperPrint,true);
- }
@RequestMapping(value = "/print")
public void print(HttpServletRequest request) throws IOException, JRException {
String ctxpath = request.getSession().getServletContext()
.getRealPath("/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper");
//得到要显示的数据
List<Report> reportList = reportService.listAc01ApplicationReport();
File reFile = new File(ctxpath);
//这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
Map parameters = new HashMap();
parameters.put('info','哈哈哈');
//封装reportList中数据作为数据源
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
JasperPrint jasperPrint = JasperFillManager.fillReport(
reFile.getPath(), parameters, ds);
//替换掉导出的代码,true为弹出打印机设置,false为直接打印
JasperPrintManager.printReport(jasperPrint,true);
}
</div>
</div>