如何设置REPORT_TIME_ZONE参数的值?

时间:2018-06-11 10:43:35

标签: jasper-reports

如何使用REPORT_TIME_ZONE内置参数设置报告的时区?

1 个答案:

答案 0 :(得分:1)

借助代码,可以轻松地将 TimeZone 传递给报告。例如,使用Java:

Map<String, Object> params = new HashMap<>();
params.put(JRParameter.REPORT_TIME_ZONE, TimeZone.getTimeZone("Europe/London"));

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());

如果您尝试直接在 jrxml 更改 REPORT_TIME_ZONE ,请执行以下操作:

<parameter name="REPORT_TIME_ZONE" class="java.util.TimeZone">
    <defaultValueExpression><![CDATA[java.util.TimeZone.getTimeZone("Europe/Copenhagen");]]></defaultValueExpression>
</parameter>

你会收到这个错误:

Caused by: net.sf.jasperreports.engine.JRException: org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 14; Error at line 10 char 14: Duplicate declaration of parameter: REPORT_TIME_ZONE

如果你真的需要在模板上设置参数,你可以使用这个技巧:

<?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="TimeZone sample" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <variable name="fake" class="java.util.TimeZone">
        <variableExpression><![CDATA[java.util.TimeZone.getDefault()]]></variableExpression>
        <initialValueExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.put("REPORT_TIME_ZONE", java.util.TimeZone.getTimeZone("Europe/Moscow"))]]> </initialValueExpression>
    </variable>
    <title>
        <band height="60">
            <textField>
                <reportElement x="0" y="30" width="100" height="30"/>
                <textFieldExpression><![CDATA[new Date()]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

在这个例子中,我通过参数Map在伪变量的帮助下改变了参数的值:

$P{REPORT_PARAMETERS_MAP}.put("REPORT_TIME_ZONE", java.util.TimeZone.getTimeZone("Europe/Moscow"))