【问题标题】:how to add data in Table of Jasper Report using Java如何使用 Java 在 Jasper 报告表中添加数据
【发布时间】:2015-07-30 13:28:58
【问题描述】:

我正在尝试使用 Java 将数据传递给 JasperReport。但是,表数据非常动态,我无法将 SQL 查询传递给报表。有什么想法吗? 我有一个 object 类型的二维数组,其中包含所有数据。我怎样才能通过呢?

ConnectionManager con = new ConnectionManager();
    con.establishConnection();

    String fileName = "Pmc_Bill.jrxml";
    String outFileName = "OutputReport.pdf";
    HashMap params  = new HashMap();
    params.put("PName", pname);
    params.put("PSerial", psrl);
    params.put("PGender",pgen);
    params.put("PPhone",pph);
    params.put("PAge",page);
    params.put("PRefer",pref);
    params.put("PDateR",dateNow);
    try {

        JasperReport jasperReport = JasperCompileManager.compileReport(fileName);

        if(jasperReport != null )
           System.out.println("so far so good ");

        // Fill the report using an empty data source

        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JRTableModelDataSource(tbl.getModel()));//con.connection);
        try{
        JasperExportManager.exportReportToPdfFile(jasperPrint, outFileName);
        System.out.printf("File exported sucessfully");
        }catch(Exception e){
            e.printStackTrace();
        }
        JasperViewer.viewReport(jasperPrint);
    } catch (JRException e) {
        JOptionPane.showMessageDialog(null, e);
        e.printStackTrace();
        System.exit(1);
    }

【问题讨论】:

  • “表数据非常动态,无法通过sql查询”是什么意思?如果您的数据保留在数据库中,则无论如何都需要查询。
  • 您可以阅读Data Source Sample 的帖子。 JRMapArrayDataSourceJRMapCollectionDataSourceListOfArrayDataSourceJRBeanArrayDataSourceJRBeanCollectionDataSource 实现数据源是您代码的理想选择。

标签: java mysql jasper-reports


【解决方案1】:
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
parameters.put("ParameterName", "Value Wanna Pass");
parameters.put("Other Parameter", Data source Name For Filling the table);

        JasperReport report = (JasperReport)JRLoader.loadObject("Path for the Jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多