3

我需要在 DynamicJasper 中创建一个包含多个包含不同列的“子报表”的报表。列的数据存储在每个行对象的 HashMap 中。我没有在 DynamicJasper 中找到任何方法来指定列的值应该来自 HashMap 上的特定键。

我发现,如果我构建一个报表,我可以扩展 JRAbstractBeanDataSource 并创建我自己的数据源,该数据源知道如何根据我如何格式化字段名称来正确获取数据。但是,当我使用 addConcatenatedReport 添加多个报告时,“子报告”使用 JRBeanCollectionDataSource 而不是我的自定义数据源。

到目前为止,我想出的唯一解决方案是拥有一个 POJO,它具有一堆属性,如“column1value”和“column2value”,我预加载并用于动态列中的字段引用。我真的不想这样做……有人能想到其他选择吗?有什么我想念的吗?

旁注:有什么想法为什么不能将自定义数据源类型传递给 addConcatenatedReport 函数?技术问题,还是根本不需要?似乎这将是“动态”报告的共同需求。

4

1 回答 1

0

I was searching for the same thing yesterday and I came across your post (and I saw it was recent). After some search I managed to overcome this problem so I thought it would be nice to share with you my findings.

I suppose that you defined an POJO for your data and then added all these objects to the HashMap (correct me if I am right).

What I did was to create a new Object that extends HashMap implementation. (In fact you dont have to extend this, you can use the HashMap object itself). So now instead of having objects inside HashMap I directly inserted values for the properties. But lets just add some code blocks in order to clear things :)

Suppose that you have the following PoJO

public class MyPOJO{

  private String name;
  private String value;
  //getters, setters etc..
 }

Instead of adding these various objects to a List and providing it as datasource you can use a HashMap this way to define your objects:

Map<String,String> myObject1=new HashMap<String,String>();
myObject1.put("name","Name1");
myObject1.put("value","Value1");

Map<String,String> myObject2=new HashMap<String,String>();
myObject2.put("name","Name2");
myObject2.put("value","Value2");

After defining these objects we can add them in a List and provide it as datasource (JRBeanCollectionDataSource). So the keys of each HashMap are considered to be the properties defined in the Columns (the properties of the initial POJO).

I dont know if my solution is the best but it worked for me!

于 2011-06-25T15:09:26.700 回答