淘先锋技术网

首页 1 2 3 4 5 6 7

在做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的代码片段:

 Connection con = null;
        try    {
            // jrxml path
            File jrxmlFile = new File("C:/reporttest/report.jrxml");
            if(jrxmlFile.exists()){
                // Compile jrxml 
                JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlFile.getAbsolutePath());
                // Set parameter
                HashMap<String, Object> params = new HashMap<String, Object>();
                params.put("KEY_ID", 10);
                ....
                // Access DB 
                Class.forName("org.postgresql.Driver");
                con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/PDFTestDD", "postgres","postgres");
                // Write DB
                JasperPrint print = JasperFillManager.fillReport(jasperReport, params, con);
                // Set file name
                File pdf = new File("c:/report.pdf");
                // Output pdf
                JasperExportManager.exportReportToPdfFile(print, pdf.getAbsolutePath());
 
                // Output XLS
                File xls = new File("c:/reportXls.xls");
                OutputStream output = new FileOutputStream(xls);
                JRXlsExporter exporterXLS = new JRXlsExporter();
                exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
                exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, output);
                exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
                exporterXLS.exportReport();
            }else{
                System.out.println("jrxml is not exsit");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(con != null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }