假设我已修复了三列(姓名,地址,电子邮件)。但是,我必须接受数据的参数List<List<String>>
。我不知道在jr:table
中使用。
我也可以使用DTO
类来解决这种情况,如下所示。
SomeDTO.java
public class SomeDTO {
private String name;
private String address;
private String email;
//getter setter
}
将List<List<String>>
转换为List<SomeDTO>
并使用new JRBeanCollectionDataSource(someDTOList)
。这不是我预期的解决方案。
参数
List<String> list_1 = new ArrayList<String>();
list_1.add("AAA");
list_1.add("AAA_Address");
list_1.add("AAA_Email");
List<String> list_2 = new ArrayList<String>();
list_2.add("BBB");
list_2.add("BBB");
list_2.add("BBB_Email");
List<List<String>> rowList = new ArrayList<List<String>>();
rowList.add(list_1);
rowList.add(list_2);
程序
public void generate(String filePath, List<List<String>> rowDataList) {
....
Map paramMap = new HashMap();
paramMap.put("TableDataSource", new JRBeanCollectionDataSource(rowDataList));
JasperPrint print = JasperFillManager.fillReport(report, paramMap);
JasperExportManager.exportReportToPdfFile(print, filePath);
}
模板
.....
<subDataset name="dynamicDataSource">
<field name="paramList" class="java.util.List"/>
</subDataset>
<parameter name="TableDataSource" class="net.sf.jasperreports.engine.JRDataSource"/>
....
<jr:table ....>
<datasetRun subDataset="dynamicDataSource">
<dataSourceExpression><![CDATA[$P{TableDataSource}]]></dataSourceExpression>
</datasetRun>
<jr:columnHeader...>
....
</jr:columnHeader>
<jr:detailCell ...>
...
<textFieldExpression><![CDATA[$F{xxxxxx}]]></textFieldExpression> <-- Here, how can I print it out?
</jr:detailCell>
</jr:table>
答案 0 :(得分:0)
您需要实现自定义数据源才能从List<List <String>>
获取数据。以下是您的数据源实现方式
import java.util.ArrayList;
import java.util.List;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
public class RowDataList implements JRDataSource {
List<List<String>> superList;
int index=-1;
Object o;
public RowDataList(List<List<String>> list) {
// pass your list here
superList=list;
}
public Object getFieldValue(JRField arg0) throws JRException {
List<String> stringList=superList.get(index);
o=stringList;
return o;
}
public boolean next() throws JRException {
index++;
return index<superList.size();
}
}
现在您可以按如下方式填写报告:
public void generate(String filePath, List<List<String>> rowDataList) {
Map paramMap = new HashMap();
//put params as per your requirement
JasperPrint print = JasperFillManager.fillReport(report, paramMap,new RowDataList(rowDataList)); // note here the custom data source is utilized
JasperExportManager.exportReportToPdfFile(print, filePath);
}
毕竟,您必须对.jrxml
文件进行细微更改。这就是看起来的样子。
.....
<subDataset name="dynamicDataSource">
<field name="paramList" class="java.util.List"/>
</subDataset>
<field name="rowDataList" class="java.lang.ArrayList"/>
....
<jr:table ....>
<datasetRun subDataset="dynamicDataSource">
<dataSourceExpression><![CDATA[$F{rowDataList}]]></dataSourceExpression>
</datasetRun>
<jr:columnHeader...>
....
</jr:columnHeader>
<jr:detailCell ...>
...
<textFieldExpression><![CDATA[$F{xxxxxx}]]></textFieldExpression> <-- Here, how can I print it out?
</jr:detailCell>
</jr:table>
你准备好了。
希望这有帮助。