您需要的JRElement
轉換爲相對JRDesign類能夠訪問該元素的特定屬性:
例
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="testJasperDesign" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="597c0716-df6b-42ec-a7c8-863eb1b7174a">
<parameter name="testParam" class="java.lang.String">
<defaultValueExpression><![CDATA["Hello world"]]></defaultValueExpression>
</parameter>
<variable name="variable1" class="java.lang.String"/>
<title>
<band height="32" splitType="Stretch">
<textField>
<reportElement x="100" y="0" width="100" height="20" uuid="bf5a8f35-3faf-457b-a6fc-b29d97a9c332"/>
<textFieldExpression><![CDATA[$P{testParam}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="100" height="20" uuid="59922664-93a3-4f69-a906-5ff418d09cd3"/>
<text><![CDATA[Static text]]></text>
</staticText>
</band>
</title>
</jasperReport>
的Java
public static void main(String[] args) throws JRException {
JasperDesign design = JRXmlLoader.load("jasper/testJasperDesign.jrxml");
JRBand titleBand = design.getTitle();
JRElement[] elements = titleBand.getElements();
for (JRElement element : elements) {
if (element instanceof JRDesignTextField){
JRDesignTextField textField = (JRDesignTextField) element;
JRExpression expression = textField.getExpression();
System.out.println(expression.getText());
}
if (element instanceof JRDesignStaticText){
JRDesignStaticText staticText = (JRDesignStaticText) element;
System.out.println(staticText.getText());
}
}
}
輸出
$ P {} testParam靜態
文本
你應該張貼的代碼 - 重現問題 –
@AlexK這不是問題。 JRElements類的文本值沒有getter。由於Jasper庫是一個開源庫,我可以創建自己的getter來獲取它嗎?如果是,那麼如何? –
'由於Jasper庫是一個開源庫,我可以創建自己的getter來獲取它嗎? - 是的。你可以做你想做的一切。這是Java –