比方说,我已经修复了三列(姓名、地址、电子邮件)。但是,我必须接受数据参数为 List<List<String>>
.我不知道在 jr:table
中使用.
我也可以使用 DTO
来解决这个问题类如下。
一些DTO.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>
最佳答案
您需要实现自定义数据源才能从 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>
你准备好了。
希望这对您有所帮助。
关于java - 如何在 JasperReports 中使用 List<List<String>> 打印表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25717189/