jasperreports6.0报表中文问题

Wesley13
• 阅读 588

        最近看了下jasperreports报表,写个helloworld一直出问题,最后发现让编码给坑了。。。

        要显示中文的话,需要itext-asian这个库,最开始加上了但是有乱码,以为是库或者字体的问题,最后发现是项目编码默认变GBK了,说好的UTF8呢!!!~

        直接发pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.blu10ph.test</groupId>
    <artifactId>TestLocalJasperReports</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.0.4</version>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
    </dependencies>

</project>

        引入jasperreports、itext和itext-asian,然后做个模板:

<?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="jasper_report_template" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c5758bb1-3f81-4404-affc-ba3b4d2185f5">
   <property name="ireport.zoom" value="1.0"/>
   <property name="ireport.x" value="0"/>
   <property name="ireport.y" value="0"/>
   <queryString>
      <![CDATA[]]>
   </queryString>
   <field name="country" class="java.lang.String">
      <fieldDescription><![CDATA[country]]></fieldDescription>
   </field>
   <field name="name" class="java.lang.String">
      <fieldDescription><![CDATA[name]]></fieldDescription>
   </field>
   <columnHeader>
      <band height="23" splitType="Stretch">
         <staticText>
            <reportElement mode="Opaque" x="0" y="3" width="535" height="15" backcolor="#70A9A9" uuid="4293b033-423d-4706-9a1d-11d62f23efb8"/>
            <box>
               <bottomPen lineWidth="1.0" lineColor="#CCCCCC"/>
            </box>
            <text><![CDATA[ ]]></text>
         </staticText>
         <staticText>
            <reportElement x="414" y="3" width="121" height="15" uuid="3f499bc9-245d-4241-a842-7edee2fde281"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
               <font fontName="宋体" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
            </textElement>
            <text><![CDATA[国家]]></text>
         </staticText>
         <staticText>
            <reportElement x="0" y="3" width="136" height="15" uuid="5bb52fb4-d212-47cc-ba60-baf104f05898"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
               <font fontName="宋体" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
            </textElement>
            <text><![CDATA[姓名]]></text>
         </staticText>
         <staticText>
            <reportElement x="36" y="-20" width="469" height="20" uuid="9bcfda11-3c52-4474-8eb5-0a1ce672df74"/>
            <textElement>
               <font fontName="宋体" size="16" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
            </textElement>
            <text><![CDATA[测试报表-中文测试-blu10ph]]></text>
         </staticText>
      </band>
   </columnHeader>
   <detail>
      <band height="16" splitType="Stretch">
         <staticText>
            <reportElement mode="Opaque" x="0" y="0" width="535" height="14" backcolor="#E5ECF9" uuid="be2670e4-26f3-4b6e-be34-ebcc23c1ee5d"/>
            <box>
               <bottomPen lineWidth="0.25" lineColor="#CCCCCC"/>
            </box>
            <text><![CDATA[ ]]></text>
         </staticText>
         <textField>
            <reportElement x="414" y="0" width="121" height="15" uuid="b80edd42-d6d1-4bff-a64e-6b463716fc25"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
               <font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{country}]]></textFieldExpression>
         </textField>
         <textField>
            <reportElement x="0" y="0" width="136" height="15" uuid="24059452-5c9f-40f0-b270-ed22993c5e26"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
               <font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
         </textField>
      </band>
   </detail>
</jasperReport>

        (我是萌萌的防止转载不留出处的URL: http://my.oschina.net/blu10phcn/blog/418014 )

        写点填充数据的代码:

class DataBean {
    private String name;
    private String country;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}


class DataBeanList {
    public ArrayList<DataBean> getDataBeanList() {
        ArrayList<DataBean> dataBeanList = new ArrayList<DataBean>();

        dataBeanList.add(produce("Manisha", "India"));
        dataBeanList.add(produce("Dennis Ritchie", "USA"));
        dataBeanList.add(produce("V.Anand", "India"));
        dataBeanList.add(produce("Shrinath", "California"));
        dataBeanList.add(produce("中文", "中国"));
        return dataBeanList;
    }

    /**
     * This method returns a DataBean object,
     * with name and country set in it.
     */
    private DataBean produce(String name, String country) {
        DataBean dataBean = new DataBean();
        dataBean.setName(name);
        dataBean.setCountry(country);
        return dataBean;
    }
}

        最后编译报表:

public class TestLocalJasperReports {
    public static void main(String[] args) {
        String sourcePath = System.getProperty("user.dir")+ File.separator+"src\\main\\resources\\reports";
        String sourceFileName = sourcePath+ File.separator+"jasper_report_template.jrxml";

        DataBeanList DataBeanList = new DataBeanList();
        ArrayList<DataBean> dataList = DataBeanList.getDataBeanList();
        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
        Map parameters = new HashMap();

        System.out.println("Compiling Report Design ...");
        JasperReport jasperReport;
        JasperPrint jasperPrint;
        try {
            jasperReport = JasperCompileManager.compileReport(sourceFileName);
            jasperPrint = JasperFillManager.fillReport( jasperReport, parameters, beanColDataSource);
            JasperExportManager.exportReportToPdfFile( jasperPrint, sourcePath+ File.separator+"jasper_report_template.pdf");
        } catch (JRException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        System.out.println("Done compiling!!! ...");
    }
}

        最近用别的语言用的比较High,忘了在学校怎么被Java的编码坑得了,一时大意害死人啊!~

        需要注意的就是项目编码一定要UTF-8,然后是报表要声明字体啊什么的,例如:

   <font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>

这个可以使用iReport进行辅助设计。

点赞
收藏
评论区
推荐文章
菜鸟阿都 菜鸟阿都
2年前
tomcat输出控制台信息乱码
使用idea开发java项目时,将其打包成war包,并使用tomcat启动,但控制台输出的中文信息乱码,与大家分享一下解决方案控制台乱码信息如下解决方案:修改文件位置:apachetomcat9.0.44\conf\logging.properties将其中的5处UTF8编码修改为GBK重启项目,idea控制台输出的tomcat部署信息乱码问题得到了解决
Wesley13 Wesley13
2年前
java编码问题总结
对于Java,由于默认的编码方式是UNICODE,所以用中文也易出问题,常见的解决是:Strings2newString(s1.getBytes(“ISO88591”),”GBK”);1、utf8解决JSP中文乱码问题一般说来在每个页面的开始处,加入:<%@pagelanguage"java"contentTyp
Stella981 Stella981
2年前
Jenkins maven 构建乱码,修改file.encoding系统变量编码为UTF
一切都是windows的控制台默认编码GBK问题情景:使用jenkins构建,console输出的中文乱码。代码编码格式是utf8,因为Jenkins会默认读取当前系统的编码格式,导致构建日志乱码和selenium自动化测试输入的中文乱码。控制台输出乱码!(https://oscimg.oschina.net/oscnet/4
Wesley13 Wesley13
2年前
Java 使用 ResourceBundle 类读取 properties 文件中文乱码的解决方案
Java使用java.util.ResourceBundle类的方式来读取properties文件时不支持中文,要想支持中文必须将文件设置为ISO88591编码格式,这对于开发工具默认为UTF8来说很不友好,而且就算用ISO88591编码,当其他人将这个项目导入开发工具时很容易出现这个properties文件中的内容有乱码(前提是该文件中包含中文)
Stella981 Stella981
2年前
Python字符编码 中文乱码
<centerPython字符编码中文乱码</centerPython中可能会出现各种各样的中文乱码问题,在python程序运行中,有三个地方与编码直接相关1.系统默认编码windows系统默认编码为GBK,Linux系统默认为UTF8(不知道是不是所有Linux都默认UTF8,但至少Ubuntu是UTF8)意味着在Li
Wesley13 Wesley13
2年前
JAVA判断文件编码类型
java读取文件,处理过程中,可能因为文件的编码问题导致了中文乱码。有时需要将UTF8的改为ANSI的编码。以下代码就可以判断文件是什么编码方式。 主要jar包:cpdetector.jar  下载地址http://cpdetector.sourceforge.net/ 同时还需jchardet1.0.jar这个包,否则detec
Stella981 Stella981
2年前
Ruby、JRuby的puts方法中文乱码问题
问题描述:从数据库中取的中文在cmd运行模式下正常显示,在rubymine中则是中文乱码,为什么?注释:只要文件的编码和终端的编码一致puts方法则可以正常显示,而puts方法是直接将字符串的字节一次输出,而没有转化为有编码格式的字符串对象。puts data'humanName'这句在
Stella981 Stella981
2年前
Beyond Compare编码
如何解决BeyondCompare中文乱码问题发布时间:20160309使用BeyondCompare比较一些含有中文的java或者jsp文件的时候,明明在项目里面是对的,可是用BeyondCompare打开之后就会出现乱码,而且看着一堆乱码还难受。那么该怎么解决GBK/UTF8等字符、
Wesley13 Wesley13
2年前
AS3中UTF
字符编码一直以来都是很混乱,非常让人令人头痛的问题,许多新手往往就会卡死在这个地方。出现乱码常见于读取、提交和写入这三个环节,稍微处理不好这个问题,结果都会演变变成乱码。归根到底是由于各家使用的编码格式不同造成的,而且往往涉及到系统本身。国内常用的网络编码格式有UTF8、GB2312、GBK,以及繁体字的BIG5,这几个编码之间的转换郁闷了好多人。在
Wesley13 Wesley13
2年前
Ubuntu下解决中文显示为方块最佳方法
一、问题分析由于Linux系统中并没有包含中文相关的字体库,而不是系统不支持中文或者中文乱码,因此显示给我们的是方块儿二、解决方法1、从window系统中的字体库复制需要的中文库到Linux系统中windows字体库路径:C:\\Windows\\Fonts这里我自己选择的是宋体simsun.ttc这个文件