jasperreports6.0报表中文问题

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

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

        直接发pom.xml:

1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5    <modelVersion>4.0.0</modelVersion> 6 7    <groupId>cn.blu10ph.test</groupId> 8    <artifactId>TestLocalJasperReports</artifactId> 9    <version>1.0-SNAPSHOT</version> 10 11    <dependencies> 12        <dependency> 13            <groupId>net.sf.jasperreports</groupId> 14            <artifactId>jasperreports</artifactId> 15            <version>6.0.4</version> 16        </dependency> 17        <dependency> 18            <groupId>com.lowagie</groupId> 19            <artifactId>itext</artifactId> 20            <version>4.2.1</version> 21        </dependency> 22        <dependency> 23            <groupId>com.itextpdf</groupId> 24            <artifactId>itext-asian</artifactId> 25            <version>5.2.0</version> 26        </dependency> 27    </dependencies> 28 29</project>

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

1<?xml version="1.0" encoding="UTF-8"?> 2<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"> 3   <property name="ireport.zoom" value="1.0"/> 4   <property name="ireport.x" value="0"/> 5   <property name="ireport.y" value="0"/> 6   <queryString> 7      <![CDATA[]]> 8   </queryString> 9   <field name="country" class="java.lang.String"> 10      <fieldDescription><![CDATA[country]]></fieldDescription> 11   </field> 12   <field name="name" class="java.lang.String"> 13      <fieldDescription><![CDATA[name]]></fieldDescription> 14   </field> 15   <columnHeader> 16      <band height="23" splitType="Stretch"> 17         <staticText> 18            <reportElement mode="Opaque" x="0" y="3" width="535" height="15" backcolor="#70A9A9" uuid="4293b033-423d-4706-9a1d-11d62f23efb8"/> 19            <box> 20               <bottomPen lineWidth="1.0" lineColor="#CCCCCC"/> 21            </box> 22            <text><![CDATA[ ]]></text> 23         </staticText> 24         <staticText> 25            <reportElement x="414" y="3" width="121" height="15" uuid="3f499bc9-245d-4241-a842-7edee2fde281"/> 26            <textElement textAlignment="Center" verticalAlignment="Middle"> 27               <font fontName="宋体" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> 28            </textElement> 29            <text><![CDATA[国家]]></text> 30         </staticText> 31         <staticText> 32            <reportElement x="0" y="3" width="136" height="15" uuid="5bb52fb4-d212-47cc-ba60-baf104f05898"/> 33            <textElement textAlignment="Center" verticalAlignment="Middle"> 34               <font fontName="宋体" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> 35            </textElement> 36            <text><![CDATA[姓名]]></text> 37         </staticText> 38         <staticText> 39            <reportElement x="36" y="-20" width="469" height="20" uuid="9bcfda11-3c52-4474-8eb5-0a1ce672df74"/> 40            <textElement> 41               <font fontName="宋体" size="16" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> 42            </textElement> 43            <text><![CDATA[测试报表-中文测试-blu10ph]]></text> 44         </staticText> 45      </band> 46   </columnHeader> 47   <detail> 48      <band height="16" splitType="Stretch"> 49         <staticText> 50            <reportElement mode="Opaque" x="0" y="0" width="535" height="14" backcolor="#E5ECF9" uuid="be2670e4-26f3-4b6e-be34-ebcc23c1ee5d"/> 51            <box> 52               <bottomPen lineWidth="0.25" lineColor="#CCCCCC"/> 53            </box> 54            <text><![CDATA[ ]]></text> 55         </staticText> 56         <textField> 57            <reportElement x="414" y="0" width="121" height="15" uuid="b80edd42-d6d1-4bff-a64e-6b463716fc25"/> 58            <textElement textAlignment="Center" verticalAlignment="Middle"> 59               <font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> 60            </textElement> 61            <textFieldExpression><![CDATA[$F{country}]]></textFieldExpression> 62         </textField> 63         <textField> 64            <reportElement x="0" y="0" width="136" height="15" uuid="24059452-5c9f-40f0-b270-ed22993c5e26"/> 65            <textElement textAlignment="Center" verticalAlignment="Middle"> 66               <font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> 67            </textElement> 68            <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression> 69         </textField> 70      </band> 71   </detail> 72</jasperReport>

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

        写点填充数据的代码:

1class DataBean { 2    private String name; 3    private String country; 4 5    public String getName() { 6        return name; 7    } 8 9    public void setName(String name) { 10        this.name = name; 11    } 12 13    public String getCountry() { 14        return country; 15    } 16 17    public void setCountry(String country) { 18        this.country = country; 19    } 20} 21 22 23class DataBeanList { 24    public ArrayList<DataBean> getDataBeanList() { 25        ArrayList<DataBean> dataBeanList = new ArrayList<DataBean>(); 26 27        dataBeanList.add(produce("Manisha", "India")); 28        dataBeanList.add(produce("Dennis Ritchie", "USA")); 29        dataBeanList.add(produce("V.Anand", "India")); 30        dataBeanList.add(produce("Shrinath", "California")); 31        dataBeanList.add(produce("中文", "中国")); 32        return dataBeanList; 33    } 34 35    /** 36     * This method returns a DataBean object, 37     * with name and country set in it. 38     */ 39    private DataBean produce(String name, String country) { 40        DataBean dataBean = new DataBean(); 41        dataBean.setName(name); 42        dataBean.setCountry(country); 43        return dataBean; 44    } 45}

        最后编译报表:

1public class TestLocalJasperReports { 2    public static void main(String[] args) { 3        String sourcePath = System.getProperty("user.dir")+ File.separator+"src\\main\\resources\\reports"; 4        String sourceFileName = sourcePath+ File.separator+"jasper_report_template.jrxml"; 5 6        DataBeanList DataBeanList = new DataBeanList(); 7        ArrayList<DataBean> dataList = DataBeanList.getDataBeanList(); 8        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList); 9        Map parameters = new HashMap(); 10 11        System.out.println("Compiling Report Design ..."); 12        JasperReport jasperReport; 13        JasperPrint jasperPrint; 14        try { 15            jasperReport = JasperCompileManager.compileReport(sourceFileName); 16            jasperPrint = JasperFillManager.fillReport( jasperReport, parameters, beanColDataSource); 17            JasperExportManager.exportReportToPdfFile( jasperPrint, sourcePath+ File.separator+"jasper_report_template.pdf"); 18        } catch (JRException e) { 19            e.printStackTrace(); 20            System.out.println(e.getMessage()); 21        } 22        System.out.println("Done compiling!!! ..."); 23    } 24}

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

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

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

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

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java编码问题总结

对于Java,由于默认的编码方式是UNICODE,所以用中文也易出问题,常见的解决是:Strings2newString(s1.getBytes(“ISO88591”),”GBK”);1、utf8解决JSP中文乱码问题一般说来在每个页面的开始处,加入:<%@pagelanguage"java"contentTyp

tomcat输出控制台信息乱码

使用idea开发java项目时,将其打包成war包,并使用tomcat启动,但控制台输出的中文信息乱码,与大家分享一下解决方案控制台乱码信息如下解决方案:修改文件位置:apachetomcat9.0.44\conf\logging.properties将其中的5处UTF8编码修改为GBK重启项目,idea控制台输出的tomcat部署信息乱码问题得到了解决

Jenkins maven 构建乱码,修改file.encoding系统变量编码为UTF

一切都是windows的控制台默认编码GBK问题情景:使用jenkins构建,console输出的中文乱码。代码编码格式是utf8,因为Jenkins会默认读取当前系统的编码格式,导致构建日志乱码和selenium自动化测试输入的中文乱码。控制台输出乱码!(https://oscimg.oschina.net/oscnet/4

Java 使用 ResourceBundle 类读取 properties 文件中文乱码的解决方案

Java使用java.util.ResourceBundle类的方式来读取properties文件时不支持中文,要想支持中文必须将文件设置为ISO88591编码格式,这对于开发工具默认为UTF8来说很不友好,而且就算用ISO88591编码,当其他人将这个项目导入开发工具时很容易出现这个properties文件中的内容有乱码(前提是该文件中包含中文)

Python字符编码 中文乱码

<centerPython字符编码中文乱码</centerPython中可能会出现各种各样的中文乱码问题,在python程序运行中,有三个地方与编码直接相关1.系统默认编码windows系统默认编码为GBK,Linux系统默认为UTF8(不知道是不是所有Linux都默认UTF8,但至少Ubuntu是UTF8)意味着在Li

jasperreports6.0报表中文问题 - HelloWorld