PDF帐票工具:JasperReports+iReport

PDF帐票工具:JasperReports+iReport

分类: JAVA POI/JXL/PDF 435人阅读 评论(0) 收藏 举报

在做PDF帐票的时候,我们用的最多的open source工具应该就是JasperReports+iReport了。

iReport is the free, open source report designer for JasperReports. Create very sophisticated layouts containing charts, images, subreports, crosstabs and much more. Access your data through JDBC, TableModels, JavaBeans, XML, Hibernate, CSV, and custom sources. Then publish your reports as PDF, RTF, XML, XLS, CSV, HTML, XHTML, text, DOCX, or OpenOffice.

JasperReports is the world's most popular open source reporting engine. It is entirely written in Java and it is able to use data coming from any kind of data source and produce pixel-perfect documents that can be viewed, printed or exported in a variety of document formats including HTML, PDF, Excel, OpenOffice and Word.

功能可以说是非常强大。

iReport 用来设计PDF模板,在java中通JasperReports来从DB取得数据并输出PDF帐票。

执行环境根据需要如下配置:

 JasperReports Requirements   
 This table contains all the libraries that JasperReports may require, depending on the functionality required by JasperReports's parent application.

JRE

  • Java Runtime Environment 5.0 or higher

Commons

JRXML

JDBC

  • JDBC 2.0 Driver

PDF

XLS

Charts


java取数据并输出PDF和Excel的代码片段:

  1. Connection con = null;  
  2.        try    {  
  3.            // jrxml path  
  4.            File jrxmlFile = new File("C:/reporttest/report.jrxml");  
  5.            if(jrxmlFile.exists()){  
  6.                // Compile jrxml   
  7.                JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlFile.getAbsolutePath());  
  8.                // Set parameter  
  9.                HashMap<String, Object> params = new HashMap<String, Object>();  
  10.                params.put("KEY_ID"10);  
  11.                ....  
  12.                // Access DB   
  13.                Class.forName("org.postgresql.Driver");  
  14.                con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/PDFTestDD""postgres","postgres");  
  15.                // Write DB  
  16.                JasperPrint print = JasperFillManager.fillReport(jasperReport, params, con);  
  17.                // Set file name  
  18.                File pdf = new File("c:/report.pdf");  
  19.                // Output pdf  
  20.                JasperExportManager.exportReportToPdfFile(print, pdf.getAbsolutePath());  
  21.   
  22.                // Output XLS  
  23.                File xls = new File("c:/reportXls.xls");  
  24.                OutputStream output = new FileOutputStream(xls);  
  25.                JRXlsExporter exporterXLS = new JRXlsExporter();  
  26.                exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);  
  27.                exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, output);  
  28.                exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);  
  29.                exporterXLS.exportReport();  
  30.            }else{  
  31.                System.out.println("jrxml is not exsit");  
  32.            }  
  33.        } catch (Exception e) {  
  34.            e.printStackTrace();  
  35.        } finally {  
  36.            if(con != null){  
  37.                try {  
  38.                    con.close();  
  39.                } catch (SQLException e) {  
  40.                    e.printStackTrace();  
  41.                }  
  42.            }  
  43.        } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值