jasperReport+iReport的国际化(I18N)

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();
    }
}
时间: 2024-12-04 12:33:14

jasperReport+iReport的国际化(I18N)的相关文章

javaweb学习总结(三十一)——国际化(i18n)

一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationalization)又称为 i18n(读法为i 18 n,据说是因为internationalization(国际化)这个单词从i到n之间有18个英文字母,i18n的名字由此而来) 二.合格的国际化软件 软件实现国际化,需具备以下两个特征: 1.对于程序中固定使用的文本元素,例如菜单栏.导航条等中使用的文本

利用JasperReport+iReport进行Web报表开发

用JasperReport+iReport进行Web报表开发 序言 在非常多实际的项目里,报表都是当中十分重要的组成部分,比如把查询结果以报表的形式呈现出来.这里所提到的报表可不是简单的二维表,而是拥有复杂表头的.多维的.能够在执行期从数据库中自己主动读取数据.可自己主动分页.拥有丰富的页面元素(图片,超连接等).支持分组和交叉表.支持打印.最好还能导出到Excel或Word…...(汗L).可是显而易见,报表功能越强大,提供的服务越丰富,其复杂度也就越提高,所以仅靠石器时代的手工方式生成报表是

springMVC项目国际化(i18n)实现方法

SpringMVC项目国际化(i18n)实现方法 按照作息规律,每周五晚必须是分享知识的时间\(^o^)/~,这周讲点儿啥呢,项目需要逼格,咱们国际化吧(* ̄rǒ ̄)~,项目中碰到这类需求的童鞋可能并不多,但偶尔用到也会比较费时间滴;-),不如驻足本帖片刻吧︿( ̄︶ ̄)︿ 额,嗯~,记得一开始从事软件开发时在一个很轻量级的项目中碰到了一个貌似叼炸天的需求,什么?国际化?没听说过,不知道怎么玩儿...,谷歌翻帖快一周,大神们的神贴也看了许久,可就是解决不了我的项目需求,怎么办,加大神Q骚扰之~~(

深入分析JavaWeb Item22 -- 国际化(i18n)

一.国际化开发概述 软件的国际化:软件开发时,要使它能同一时候应对世界不同地区和国家的訪问,并针对不同地区和国家的訪问.提供对应的.符合来訪者阅读习惯的页面或数据. 国际化(internationalization)又称为 i18n(读法为i 18 n,据说是由于internationalization(国际化)这个单词从i到n之间有18个英文字母,i18n的名字由此而来) 二.合格的国际化软件 软件实现国际化,需具备以下两个特征: 1.对于程序中固定使用的文本元素.比如菜单条.导航条等中使用的

spring 国际化-i18n

i18n(其 来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版 物,软件,硬件等)无需做大的改变就能够适应不同的语言和地区的需要.对程序来说,在不修改内部代码的情况下,能根据不同语言及地区显示相应的界面. 在全球化的时代,国际化尤为重要,因为产品的潜在用户可能来自世界的各个角落.通常与i18n相关的还有L10n(“本地化”的简称).<摘自百度 百科http://baike.baidu.c

jasperReport+ireport实现报表导出和数据分析

jasperReport + ireport组合实现报表文件(各种文件格式xsl.pdf,rtf等)的生成导出是目前开源报表中使用的最为广泛的.在平台一期完成之后,而且功能中报表以及数据分析这块是必不可少的,一下展示如何使用JasperReport + ireport导出excel报表. 1.需要.jasper文件作为生成报表的模板,这个文件是由ireport文件编译.jrxml文件之后生成的.这个需要注意的是JasperReport和ireport的版本一定要对应,否则很可能出问题.下面的文件

python国际化 i18n 和中英文切换

Python通过gettext模块支持国际化(i18n),可以实现程序的多语言界面的支持,下面是我的多语言支持实现: 1.         在python安装目录下的./Tools/i18n/(windows下例 D:\Program Files\Python25\Tools\i18n)目录中找到pygettext.py运行之,生成翻译文件模版messages.pot,内容大概是这个样子: # SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR ORGANI

浅谈struts2的国际化----i18n

可能大家在使用struts框架的时候,偶尔会看到这个词: i18n.也就是 Internationalization    i 开头,n 结尾, 总共18个字母,今天的主要内容就是围绕这 四个字母. struts2国际化是建立在 java 国际化的基础上的,通过提供不同国家.语言环境的消息资源,然后通过 ResourceBundle 加载指定Locale 对应的资源文件,取得文件中指定的 key 对应的信息. Locale 对象表示了特定的地理.政治和文化地区.需要 Locale 来执行其任务的

使用JasperReport+iReport进行Web报表开发

前言 在实际工程中非常,报告是其中很重要的一部分,结果以报表的形式呈现出来.这里所提到的报表可不是简单的二维表,而是拥有复杂表头的.多维的.能够在执行期从数据库中自己主动读取数据.可自己主动分页.拥有丰富的页面元素(图片.超连接等).支持分组和交叉表.支持打印.最好还能导出到Excel或Word…...(汗L). 可是显而易见,报表功能越强大,提供的服务越丰富,其复杂度也就越提高,所以仅靠石器时代的手工方式生成报表是不能满足须要的. 所幸,眼下我们所熟知的几款报表工具功能上足够强大,并且都附有非