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