ireport是java项目中比较好用的pdf报告生成工具,可以通过JRBeanCollectionDataSource传递java对象至ireport。在项目比较小的时候是比较快捷的。当项目需求复杂了,人员增多了,这种方式不适合任务细分,前后流程衔接太紧密,而且不好调试。
采用Mysql作为中间数据交换,能直接在ireport中预览数据展示结果。而且前后端解耦,可以将任务分派给不同的人来做。

一、配置数据库

点击下图的红框:

新建一个Database JDBC connection:

配置好MySQL的信息,Test成功!

二、使用 JDBC连接

1,SQL与Java Object的对应关系

2,SQL Qurey

点击下图的红框:

编写SQL语句,点击“Read Fields”,会显示查询的字段,点击OK,字段会在左侧的Fields中显示出来。

Fields会根据sql查询的字段自动生成:

在Detail报表中调用Fields:

Preview预览如下:

三、在Java项目中调用


需要先在jrxml中设置语言为java,否则会报如下的错误:。

1
java.lang.NoClassDefFoundError: org/codehaus/groovy/control/CompilationFailedException

java文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
/**
* 报告
* @author dzm
*
*/
@Controller
@RequestMapping("productReport")
public class ProductRepotController extends BaseController{
/**
* 一建生成报告
* parmas: fileFormat: pdf or doc
*/
@RequestMapping(value = "exportReport", method = RequestMethod.GET)
public void exportReport(String fundId, String fundName, String fileFormat, HttpServletResponse response){
Date curDate = new Date();
String dateStr = DateUtil.formatDate(curDate, "yyyy-MM-dd");
// 文件路径
StringBuilder filePath = new StringBuilder("");
filePath.append(upload_path).append(dateStr).append("/").append(fundId);
FileUtil.createDictionary(filePath.toString());
// 文件名
StringBuilder fileName = new StringBuilder("");
fileName.append(fundName).append("报告_").append(dateStr).append(".").append(fileFormat);
// 文件全路径
StringBuilder fullName = new StringBuilder("");
fullName.append(filePath).append("/").append(fileName);
// 加载报表对象
InputStream is = null;
JasperReport jasperReport = null;
JasperPrint jasperPrint = null;
is = ProductDetailController.class.getClassLoader().getResourceAsStream(analysis_jasper_path);
try{
// 响应头部信息设置
response.setContentType("text/plain");
response.setHeader("Content-Disposition", "attachment; filename="+FileUtil.getAttachName(fileName.toString()));
//
jasperReport = (JasperReport) JRLoader.loadObject(is);
Map<String,Object> params = new HashMap<String,Object>();
params.put("SUBREPORT_DIR", getClassPath(subreport_dir));
params.put("IMAGE_DIR", "file:"+filePath+"/");
//
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?"
+ "user=s1&password=s1&useUnicode=true&characterEncoding=UTF8";
Connection conn = (Connection) DriverManager.getConnection(url);
jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);
if (fileFormat.equals("pdf")) {
JasperExportManager.exportReportToPdfFile(jasperPrint, fullName.toString());
}else{
JRRtfExporter docReport = new JRRtfExporter();
docReport.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,fullName.toString());
docReport.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
docReport.exportReport();
}
FileUtil.download(fullName.toString(), response.getOutputStream());
} catch(Exception e){
e.printStackTrace();
logger.error("生成报告失败,失败原因:{}",e.getMessage());
} finally{
try{
if (is!=null) is.close();
}catch(Exception e){
logger.error("关闭文件输入流异常:{}",e.getMessage());
}
}
}
}

参考

https://my.oschina.net/jiangli0502/blog/122885
http://stackoverflow.com/questions/8935925/jasperreports-compilation-error