最近看了下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进行辅助设计。