在JAVA SWING项目中需要用到报表打印
1.下载iReport
2.安装-新建
3.下面是我的报表DEMO
4.报表格式
5.使用JavaBean作为数据源
数据-数据源连接-添加-JavaBean数据源-下一步-输入对应的javabean类名称(包括包名)-测试-连接成功即可
6.选择数据库 如图
7.在弹出的报表查询中--JavaBean数据源
类名中输入刚才输入的类名-读取属性--确定
8.即可回到4中,将字段拖到报表中。
9.项目中
private JRViewer jrview = null;
private JasperPrint jasperPrint;
private JasperReport report;
JRDataSource ds = new JRBeanCollectionDataSource(list); list即JAVABEAN的数据列表
String reportFilePath = this.getClass()
.getResource("fkebean.jasper").getPath();
// 生成JasperPrint
report = (JasperReport) JRLoader.loadObject(reportFilePath);
// 用参数传递过去的 hm
jasperPrint = JasperFillManager.fillReport(report, hm, ds);//hm是hashMap用力传递参数
jrview = new JRViewer(jasperPrint);
panel.add(BorderLayout.CENTER, jrview);
10.打印
// 执行打印操作
JasperPrintManager.printReport(jasperPrint, false);//false表示不显示打印设置窗口
============如果用JDBC作为数据源的话,那么====================
Connection con;
// 加载报表模版
InputStream inReport = PrintFrame.class
.getResourceAsStream("fke2.jrxml");
jasperReport = JasperCompileManager.compileReport(inReport);
// 建立连接
con = ConnectionManager.getConnection();
/jasperPrint = JasperFillManager.fillReport(filePath, hm, ds);
// 在使用iReport设计了报表之后可以使用连接对象直接填充报表。
jasperPrint = JasperFillManager.fillReport(jasperReport, hm, con);
// 打印预览
JasperViewer.viewReport(jasperPrint);
// 打印
JasperPrintManager.printReport(jasperPrint, false);
ConnectionManager.close(con);