我正在尝试通过将自定义数据源传递给表格来填充表格。我创建了一个带有表格的简单报告。它自己的报告从 ms sql 数据库中获取数据。我编写了一个类似于此示例中的类的 java 类。但我在表中没有任何价值。在示例中没有 scriptlet。我检查了(String) this.getFieldValue("KN_FormelGG");
代码行。它从现场获取数据并可以在报告中显示。所以我猜是bean数据源没有填满。我在afterGroupInit
. 如何在 jasper 中使用来自 java 的数据集合?我还尝试在数据集和查询对话框中添加 java bean ,但它也没有帮助我。我应该将脚本添加到子报表/表吗?我的问题的主要重点是在 scriptlet 中拥有自定义数据源。我去了其他问题通过,但我仍然没有得到答案。我添加$P{FieldDataSource}.getData()
以检查数据,但它提供空值。
Java类1:
package testProjektIman.scriptlets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
public class FillTable extends JRDefaultScriptlet {
@Override
public void afterGroupInit(final String id)
throws JRScriptletException {
fillTable();
}
public ArrayList<String> splitGGArray(final String kNFormelGG) {
ArrayList<String> fieldNames = new ArrayList<>();
String[] array = (kNFormelGG.split(" "));
for (String sub : array) {
fieldNames.add(sub);
}
return fieldNames;
}
public Map<String, Object> fillTable()
throws JRScriptletException {
String kNFormelGG = null;
kNFormelGG = (String) this.getFieldValue("KN_FormelGG");
List<TableCells> listTableCells = new ArrayList<>();
TableCells tableCell;
for (String fn : splitGGArray(kNFormelGG)) {
tableCell = new TableCells();
tableCell.setFieldName(fn);
listTableCells.add(tableCell);
}
JRBeanCollectionDataSource tableCellJRBean = new JRBeanCollectionDataSource(listTableCells);
Map<String, Object> parameters = new HashMap<>();
parameters.put("FieldDataSource", tableCellJRBean);
return parameters;
}
}
Java 类 2
package testProjektIman.scriptlets;
public class TableCells {
private String fieldName;
private String keyFormel;
private String mK;
private String notation;
private String Item;
//getters setters
}
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="tableAutoFill" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="37fc3a9c-38e9-41be-9039-56249c5283d7">
<subDataset name="TableDataSource" uuid="5999e646-aeec-4b2b-b29b-68e897d56999">
<queryString>
<![CDATA[]]>
</queryString>
<field name="fieldName" class="java.lang.String"/>
</subDataset>
<scriptlet name="Filltable" class="testProjektIman.scriptlets.FillTable"/>
<parameter name="FieldDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource" isForPrompting="false"/>
<queryString>
<![CDATA[select * from "KennzahlReferenz2015_QIBericht", "Images"
where LB_ID = 62
and KN_OffiziellGruppe = 3
and IMG_ID = 1]]>
</queryString>
<field name="KN_Id" class="java.lang.Integer"/>
<field name="KN_FormelZ" class="java.lang.String"/>
<field name="KN_FormelGG" class="java.lang.String"/>
<group name="id">
<groupExpression><![CDATA[$F{KN_Id}]]></groupExpression>
<groupHeader>
<band height="50"/>
</groupHeader>
<groupFooter>
<band height="50"/>
</groupFooter>
</group>
<detail>
<band height="191" splitType="Stretch">
<componentElement>
<reportElement x="50" y="18" width="260" height="120" uuid="942ab836-df83-4a2f-8215-845073ad163f">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
</reportElement>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" whenNoDataType="AllSectionsNoDetail">
<datasetRun subDataset="TableDataSource" uuid="b554ada5-c388-4534-af8e-93571a417adb">
<parametersMapExpression><![CDATA[$P{FieldDataSource}]]></parametersMapExpression>
<dataSourceExpression><![CDATA[$P{FieldDataSource}]]></dataSourceExpression>
</datasetRun>
<jr:column width="100" uuid="f57e4e8e-8a2e-405f-b9ab-7a704e0986fd">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="9a55dd73-71e3-4559-9a8b-17d98bf17753"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[FieldName]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="c3e6ccfc-9f91-4d7a-800a-613f5dded928"/>
<textFieldExpression><![CDATA[$F{fieldName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>