我正在尝试使用iReport Designer传递和打印动态2D数组对象。
我的简单数据源如下(这是动态的,实时有n行和列):
Object [][] myData = new Object [][] {{"RECORD1"}, {"RECORD2"}};
我把它传递给JasperDataSourceBuilder
班:
我为此写了以下代码:
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
public class JasperDataSourceBuilder implements JRDataSource {
private static int rowIndex = 0;
private static int columnIndex = -1;
private Object[][] data = null;
public JasperDataSourceBuilder(Object[][] dataObject){
data = dataObject;
}
@Override
public boolean next() throws JRException {
boolean hasMore = false;
if((data != null && data.length > 0)){
if(rowIndex < data.length){
columnIndex++;
if(columnIndex < data[rowIndex].length){
hasMore = true;
}else{
rowIndex ++;
columnIndex = 0;
hasMore = true;
}
}
}
return hasMore;
}
@Override
public Object getFieldValue(JRField arg0) throws JRException {
return String.valueOf(data[rowIndex][columnIndex]);
}
public Object[][] getData() {
return data;
}
public void setData(Object[][] data) {
this.data = data;
}
}
当我调用report()
方法时,会生成PDF,但报告中只会打印第一行。
我的jrxml如下
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4f311e4c-b629-4c6c-a718-451aec9c679e">
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#A9A9A9">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#FFBFBF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table 1">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table 1_TH" mode="Opaque" backcolor="#BBA8A8">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table 1_CH" mode="Opaque" backcolor="#FFE6E6">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
<style backcolor="#FFF8F8"/>
</conditionalStyle>
</style>
<style name="table 2">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table 2_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table 2_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table 2_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="dataset1" uuid="15c5b3b7-2873-4678-a6ad-67db2344acaa"/>
<field name="field1" class="java.lang.String">
<fieldDescription><![CDATA[RECORD HEADER]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="94" y="2" width="100" height="20" uuid="cdfaad19-02d4-4846-9b06-c71a7df3c92f"/>
<text><![CDATA[RECORD HEADER]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125">
<textField>
<reportElement x="94" y="55" width="100" height="20" uuid="d154b00a-f10d-4803-9901-4580080fc982"/>
<textFieldExpression><![CDATA[$F{field1}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
我想在我的应用程序中使用JasperReports,目前我使用的是itextPDF,其中我的数据是所有二维对象,它显示带有Headers&amp;的表。数据
我希望将此Object[][]
作为数据源保留,并在现有报告中实施。
答案 0 :(得分:0)
我可以成功使用Object [] []作为数据源。
在下面的类中,我迭代我的2D对象&amp;返回值。 getFieldValue方法只是从2D对象的下一个索引返回正确的值。 (确保将JRXML中的列字段的数量与2D对象匹配)
public class JasperDataSourceBuilder implements JRDataSource {
private int rowIndex = 0;
private int columnIndex = 0;
private Object[][] data = null;
public JasperDataSourceBuilder(Object[][] dataObject){
data = dataObject;
}
@Override
public boolean next() throws JRException {
boolean hasMore = false;
if((data != null && data.length > 0)){
if(rowIndex < data.length){
if(columnIndex < data[rowIndex].length){
hasMore = true;
}else{
rowIndex ++;
columnIndex = 0;
hasMore = true;
}
}
}
if(rowIndex >= data.length){
hasMore = false;
}
return hasMore;
}
@Override
public Object getFieldValue(JRField arg0) throws JRException {
String fieldVal = String.valueOf(data[rowIndex][columnIndex]);
columnIndex++;
return fieldVal;
}
public Object[][] getData() {
return data;
}
public void setData(Object[][] data) {
this.data = data;
}
}
然后使用以下代码生成报告 -
jasperPrint = JasperFillManager.fillReport(
reportStream, parameters, new JasperDataSourceBuilder(dataObject));
String fileName = "report2.PDF";
response.setContentType("application/pdf;charset=ISO-8859-1");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setHeader("Cache-Control", "max-age=0");
OutputStream ouputStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream);
ouputStream.flush();
ouputStream.close();
上述内容可以通过扩展 AbstractTableModel 来实现,但是对于我的要求&amp;简单性我将如上所述实现 JRDataSource 。
我发现上面评论中的参考链接非常有用。
希望这有助于JasperReports的一些新手。
谢谢!