问题
In a jasper report, with iReports Designer one can set the Report Name to some value (in iReports, this is on the most top node in the report inspector).
How can the value of this parameter been accessed later within e.g. a variable or a textfield of the same report?
回答1:
With help of JASPER_REPORT parameter we can get instance of JasperReport class. This is the current template object.
With help of JasperReport.getName() method we can get the report name.
Example of template
<?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="Show the report name example" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<title>
<band height="50" splitType="Stretch">
<textField>
<reportElement x="110" y="0" width="290" height="25"/>
<textFieldExpression><![CDATA[$P{JASPER_REPORT}.getName()]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
In this example the name of report is: name="Show the report name example"
The output result in Studio
回答2:
The answer from Alex K is correct. I just want to add additional information on how to do this in Jasper Scriplets as it might help people finding this answer:
String nameOfReport = ((JasperReport)getParameterValue(JRFillParameter.JASPER_REPORT)).getName(); // Name of report
String fullReportUnitPath = ((JasperReport)getParameterValue(JRFillParameter.JASPER_REPORT)).getProperty("ireport.jasperserver.reportUnit"); // path to report unit
// for getting the folder name above the report file a.k.a reportunit
int indexOfLastPathSlash = fullReportUnitPath.lastIndexOf("/");
String nameOfReportUnit = fullReportUnitPath.substring(indexOfLastPathSlash+1);
来源:https://stackoverflow.com/questions/57126535/how-to-access-report-name-attribute-from-within-a-report