简体   繁体   English

JasperReport 如何从地图初始化 JRDataSource <string, list<string> &gt;</string,>

[英]JasperReport How to initialize a JRDataSource from a Map<String, List<String>>

I am trying to create a pdf file with a simple table.我正在尝试用一个简单的表格创建一个 pdf 文件。 I have computed all the table data ahead into a Map<String, List<String>> where the keys are the table columns and the corresponding list is the values that will populated each column cells.我已经将所有表数据提前计算到一个Map<String, List<String>>中,其中键是表列,相应的列表是将填充每个列单元格的值。 I have followed this example but I seem to not be able to correctly initialize the JRDataSource required to print the report since I can't understand what it expects to receive.我遵循了这个例子,但我似乎无法正确初始化打印报告所需的 JRDataSource,因为我无法理解它期望收到的内容。 It seems that are several classes that implement the interface like JRMapArrayDataSource or JRMapCollectionDataSource but those expect an array or a collection.似乎有几个类实现了JRMapArrayDataSourceJRMapCollectionDataSource等接口,但那些类需要一个数组或一个集合。 I have already tried to convert my map to one of these but that won't work because then it can't know what goes to each column.我已经尝试将我的地图转换为其中一个地图,但这行不通,因为它无法知道每一列的内容。 Follows my code.按照我的代码。 Any clue on how to structure this would be very appreciated.任何关于如何构建它的线索将不胜感激。

public void buildReport(Map<String, List<String>> tableData) throws Exception {
    drb = new DynamicReportBuilder();

    List<AbstractColumn> columns = new ArrayList<>();

    tableData.keySet().forEach(key-> {
        AbstractColumn column = ColumnBuilder.getNew()
            .setColumnProperty(key, String.class.getName()).setTitle(key.toUpperCase()).setWidth(85).build();

        columns.add(column);
    });

    columns.forEach(column -> drb.addColumn(column));

    DynamicReport dynamicReport = drb.build();

    JasperReport jasperReport = DynamicJasperHelper.generateJasperReport(dynamicReport,
            new ClassicLayoutManager(),
            params);

    //JRDataSource dataSource = new JRBeanCollectionDataSource(SortUtils.sortCollection(Arrays.asList(tableData.values()), dynamicReport.getColumns()));
    JRDataSource dataSource = new JRMapArrayDataSource(tableData); // this is wrong since it expects java.lang.Object

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);

    exportReport(jasperPrint, System.getProperty("user.dir")+ urlJasper + "reports/medical-report.pdf");

}

I fixed the issue for this case by restructuring the data into a collection of rows.我通过将数据重组为行集合来解决此案例的问题。 So instead of having a Map<String, List<String>> I had to convert it to a List<Map<String, String>> where each map in the list concerns a line in the table and the map has the column name as key and the column value for the row as value.因此,我不得不将其转换为List<Map<String, String>>而不是Map<String, List<String>> ,其中列表中的每个映射都涉及表中的一行,并且映射的列名称为行的键和列值作为值。 I then used然后我用

JRDataSource dataSource = new JRBeanCollectionDataSource(tableData);

to create the JRDataSource.创建 JRDataSource。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在jasperReport中对字符串列求和 - How to claculate string column Sum in jasperReport 如何映射地图 <String, List<String> &gt;在休眠状态 - How to map Map<String, List<String>> in hibernate 如何转换地图 <String, List<String> &gt;到地图 <String, String> 在java 8中 - How to Convert a Map<String, List<String>> to Map<String, String> in java 8 初始化地图 <String, Object> 来自地图条目的实例 - Initialize Map<String, Object> instance from Map entries 如何从列表中过滤并转换为 Map<String,String> - How to, from a list, filter it and convert to Map<String,String> 如何转换列表<map<string, object> > 列出<map<string, string> > </map<string,></map<string,> - How to Convert List<Map<String, Object>> to List<Map<String, String>> 如何使用Java 8 Stream从Map列表中的字符串中获取列表? - How to get a List from string from List of Map with Java 8 Stream? 地图速度表 <String, List<String> &gt; - Velocity table from Map<String, List<String>> 从列表中获取值<Map<String, String> &gt; - Get values from List<Map<String, String>> 如何转换列表<map<string, string> &gt; 列出<map<string, map<string, string> &gt;&gt; </map<string,></map<string,> - how to convert List<Map<String, String>> to List<Map<String, Map<String, String>>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM