【问题标题】:How to generate an ireport according user input in netbean gui如何根据 netbean gui 中的用户输入生成 ireport
【发布时间】:2014-04-16 05:15:10
【问题描述】:
CREATE TABLE Account(
    account_no int,
    balance real,
    type_code int,
    branch_no int,
    Bcode int,
    customer_no int,

    CONSTRAINT account_pk PRIMARY KEY (account_no),

);

当用户将type_code(account type code)branch_no 作为 GUI 输入时,我想生成 Jasper 报告,然后 ireport 应显示上述帐户表的所有帐户详细信息。

我不知道该怎么做。谁能帮帮我?

【问题讨论】:

    标签: java mysql netbeans jasper-reports ireport


    【解决方案1】:

    首先,您没有指定要创建的应用程序类型,所以我的回答有点笼统。此外,您也没有提到您是否已经设法构建了您的第一份报告(我的意思是,没有接受任何用户输入)。因此,下面我将展示生成 JasperReport 所需的部分:

    public void generateReport(ActionEvent actionEvent) throws FileNotFoundException {
    
    JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(PopulateBean.createBeanCollection());
    Map parameters = new HashMap();
     
    try {
        InputStream is = new FileInputStream(new File("Source path to template.jrxml"));
        OutputStream os=new FileOutputStream(new File("Resulting report.pdf"));
         
        JasperDesign jasperDesign = JRXmlLoader.load(is);
        JasperReport jasperReport =
            JasperCompileManager.compileReport(jasperDesign);
    
        JasperPrint jasperPrint =
            JasperFillManager.fillReport(jasperReport, parameters, ds);
    
        JasperExportManager.exportReportToPdfStream(jasperPrint, os);
    } catch (JRException e) {
          e.printStackTrace();
    }
    

    }

    此代码应集成到​​您的应用程序中。您要求的部分是:

    Map parameters = new HashMap();
    

    您只需将用户插入的输入放入此地图中。例如,如果你有一个 JSF 页面,那么你可以获取它的 UI 组件的值并存储在这个地图中

    parameters.put("type_code", getTypeCodeUIComponent().getValue());
    

    你会在上面的代码中看到这张地图被传递给了报告:

    JasperFillManager.fillReport(jasperReport, parameters, ds);
    

    剩下的唯一事情就是在 iReport 中编辑您的报告查询。首先,您创建一个与插入到地图中的名称完全相同的参数(在本例中为“type_code”。注意,它区分大小写)。其次,您应该使用 WHERE 子句,根据此参数过滤类型列,如下所示: 还有一些教程:12

    希望这些有帮助!

    【讨论】:

    • 我可以在没有 gui 输入的情况下生成报告。我的 GUI 快照附在链接 drive.google.com/file/d/0B9tyULY_FLygalZXdmRtY29NY3c/… 链接 下方,您的代码会生成如下错误。drive.google.com/file/d/0B9tyULY_FLygdXFLR2xmT0tRMFE/… 页面链接 我是 Java 编程的初学者。谢谢你的精彩解释
    • PopulateBean 是我在上面链接的第二个教程中实现的一个类。而 createBeanCollection() 是一个静态方法。当然你没有这样的类或方法。因此,您可以将整个“PopulateBean.createBeanCollection()”替换为另一个数据源(例如列表)。或者您可以删除整行:“JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(PopulateBean.createBeanCollection());”并在 JasperFillManager 中设置 null 而不是 ds
    • 好的,我会按照教程进行操作,如果出现任何问题,我会与您联系。谢谢。
    猜你喜欢
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    相关资源
    最近更新 更多