【问题标题】:DynamicJasper: use array or list as datasource without objectsDynamicJasper:使用数组或列表作为没有对象的数据源
【发布时间】:2019-06-29 10:38:53
【问题描述】:

我正在使用DynamicJasper 来构建报告,但我在这里面临的唯一问题是您需要将对象集合作为数据源传递。
但就我而言,我需要传递动态列和数据,例如: 检查此链接http://dynamicjasper.com/documentation-examples/getting-started/: 在“Creating a simple Report”部分下,他们将Product 列表作为数据源传递:

FastReportBuilder drb = new FastReportBuilder();
DynamicReport dr = drb.addColumn("State", "state", String.class.getName(),30)
.addColumn("Branch", "branch", String.class.getName(),30)
.addColumn("Product Line", "productLine", String.class.getName(),50)
.addColumn("Item", "item", String.class.getName(),50)
.addColumn("Item Code", "id", Long.class.getName(),30,true)
.addColumn("Quantity", "quantity", Long.class.getName(),60,true)
.addColumn("Amount", "amount", Float.class.getName(),70,true)
.addGroups(2)
.setTitle("November 2006 sales report")
.setSubtitle("This report was generated at " + new Date())
.setPrintBackgroundOnOddRows(true)
.setUseFullPageWidth(true)
.build();

JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());// here they give list of Products
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
JasperViewer.viewReport(jp);

在此示例中,他们使用了 Product 实体列表,其中该实体具有 statebranch 等属性,因此它可以匹配给定的列。

但就我而言,我将获取动态数据,它不是对象列表,而是数据数组(字符串、整数、日期等...)

我的问题是:有没有办法将自定义列表传递给 JRBeanCollectionDataSource 而不是预定义对象列表?

【问题讨论】:

    标签: java dynamic-jasper


    【解决方案1】:

    解决方案是使用JRMapCollectionDataSource 而不是JRBeanCollectionDataSource
    这样我可以像这样传递数据:

    List<Map<String, ?>> list = new ArrayList<>();
    Map<String, Object> row = new HashMap<>();
    row.put("state", "State value");
    row.put("branch", "Branch value");
    row.put("productLine", "Product line value");
    // add the row to the list
    list.add(row);
    
    // and finaly pass the list to datasource
    JRDataSource ds = new JRMapCollectionDataSource(list);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 2011-09-22
      相关资源
      最近更新 更多