标签:
JasperReport中,也可以根据不通的Locale来将个别信息进行国际化处理。根据我的整理,应该有以下的三种方式:
(1)采用PrintWhenExpression的方式:PrintWhenExpression属性,是指在打印的时候,会根据设定的Expression(只返回布尔值)进行判定,如果为true,那么就打印信息;如果为false,那么该信息就直接不打印。采用该种方式来实现(一般是用StaticText来),需要几种国际化语言,对应的元素就要出现几次,每个元素的PrintWhenExpression根据需要设置,比如zh_CN==true这个简体中文的元素显示,zh_CN为false则显示繁体中文的那个元素,如下:
查看XML,两个staticText的x、y和width,包括其他属性都是一模一样,除了PrintWhenExpression表达式:
<staticText> <reportElement x="0" y="0" width="555" height="39" uuid="3a479cfe-3b0b-42c3-91aa-4689e74e5e90"> <printWhenExpression><![CDATA[$P{REPORT_LOCALE}.getCountry().equals("CN")]]></printWhenExpression> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <text><![CDATA[这个是采用PrintWhenExpression来实现的]]></text> </staticText> <staticText> <reportElement x="0" y="0" width="555" height="39" uuid="3a479cfe-3b0b-42c3-91aa-4689e74e5e90"> <printWhenExpression><![CDATA[!$P{REPORT_LOCALE}.getCountry().equals("CN")]]></printWhenExpression> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <text><![CDATA[這個是採用PrintWhenExpression來實現的]]></text> </staticText>
(2)在元素上采用Expression实现,采用TextField,设置Expression表达式诸如:$P{REPORT_LOCALE}.getCountry().equals("CN")?"这个是通过Expression来控制的":"這個是通過Expression來控制的"。当然,前提是language是Java,如果是Groovy或者其他的没有equals这个方法;
(3)采用resource bundle的形式实现:该方法需要在报表上设置resource bundle的属性,属性值为要读取的i18n文件的名字,然后需要在classpath下新增对应的国际化文件,然后右击该元素,Edit EXpression,将表达式设置为$R{properties文件中对应的key},如下:
a.
b.classpath下添加对应的国际化文件,文件名为上一步resource bundle的值+"_"+Locale(比如zh_CN,zh_TW),如下:
c.iReport中右击该元素,edit expression,填写$R{my.detail}即可完成。
最后来一张最终导出的PDF,包含了三种,虽然实现方式不同,结果一致:
附下jrxml文件和Java:
<?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="i18n" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="i18n" uuid="49274885-3989-4457-a0db-36c6ac64ff1b"> <property name="ireport.zoom" value="1.0"/> <property name="ireport.x" value="0"/> <property name="ireport.y" value="0"/> <background> <band splitType="Stretch"/> </background> <title> <band height="39" splitType="Stretch"> <staticText> <reportElement x="0" y="0" width="555" height="39" uuid="3a479cfe-3b0b-42c3-91aa-4689e74e5e90"> <printWhenExpression><![CDATA[$P{REPORT_LOCALE}.getCountry().equals("CN")]]></printWhenExpression> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <text><![CDATA[这个是采用PrintWhenExpression来实现的]]></text> </staticText> <staticText> <reportElement x="0" y="0" width="555" height="39" uuid="3a479cfe-3b0b-42c3-91aa-4689e74e5e90"> <printWhenExpression><![CDATA[!$P{REPORT_LOCALE}.getCountry().equals("CN")]]></printWhenExpression> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <text><![CDATA[這個是採用PrintWhenExpression來實現的]]></text> </staticText> </band> </title> <pageHeader> <band height="35" splitType="Stretch"/> </pageHeader> <columnHeader> <band height="39" splitType="Stretch"> <textField> <reportElement x="0" y="0" width="555" height="39" uuid="b988adc9-7631-4230-b93e-213e90c2ed5f"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <textFieldExpression><![CDATA[$P{REPORT_LOCALE}.getCountry().equals("CN")?"这个是通过Expression来控制的":"這個是通過Expression來控制的"]]></textFieldExpression> </textField> </band> </columnHeader> <detail> <band height="125" splitType="Stretch"> <textField> <reportElement x="0" y="16" width="555" height="48" uuid="bcdebbfa-12f5-42cf-8bdd-404b27651d91"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <textFieldExpression><![CDATA[$R{my.detail}]]></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>
package com.zzq.test.myjasper; import java.io.File; import java.util.HashMap; import java.util.Locale; import java.util.Map; import net.sf.jasperreports.engine.JREmptyDataSource; import net.sf.jasperreports.engine.JRParameter; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.util.JRLoader; import net.sf.jasperreports.export.SimpleExporterInput; import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput; public class I18N { private static final String NAME = System.getProperty("java.class.path").split(";")[0] + File.separator + "i18n"; /** * 将jrxml编译成jasper的格式,并保存在classpath目录下 * @return * @throws Exception */ public static void compile() throws Exception { JasperCompileManager.compileReportToFile(NAME + ".jrxml"); } /** * 将数据填充到编译后的jasper,重新生成一个jrprint的文件 * @throws Exception */ public static void fillData() throws Exception { Map<String, Object> params = new HashMap<String, Object>(); params.put(JRParameter.REPORT_LOCALE, Locale.TAIWAN); //此处不能直接用JasperFillManager.fillReportToFile(NAME + ".jasper", params)调用,需要多传一个JREmptyDataSource,否则导出的文件有问题,具体PDF空白,excel文件有问题 JasperFillManager.fillReportToFile(NAME + ".jasper", params, new JREmptyDataSource()); } /** * 将报表导出成pdf * @throws Exception */ public static void printPDF() throws Exception { JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(new File(NAME + ".jrprint")); File destFile = new File(NAME + ".pdf"); JRPdfExporter exporter = new JRPdfExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile)); exporter.exportReport(); } public static void main(String[] args) throws Exception { compile(); fillData(); printPDF(); } }
jasperReport+iReport的国际化(I18N)
标签:
原文地址:http://my.oschina.net/zzq350623/blog/521351