前言
数据库实验要求要打印报表,使用了JasperReport+iReport,一开始想着应该不难,没有用maven,遭受了一晚上jar依赖的毒打,只好配上了maven。终于导出之后,又被中文无法显示一顿暴揍,我是弟弟。。。一、iReport
- 设计好报表之后(还没设计好,推荐看这一篇文章:Java报表小记之 JasperReports & iReport),需要设置一下字体和编码方式:(可以为中文的文本框可以多选之后一起设置)
设置完成之后,编译为.jasper文件。 - 找依赖包,因为jasperReport不支持中文,so,我们还是要去把宋体中文包的依赖导进去。
打开iReport的安装路径
在iReport-5.6.0\ireport\modules\ext目录下找到:iTextAsian.jar,复制出来
到iReport-5.6.0\ireport\fonts目录下,可以看到irfonts.xml 和 jasperreports_extension.properties两个文件,在该目录下打开cmd,然后输入命令jar cvf simsun.jar * 打包为simsun.jar
okk,到此我们就获取了两个jar包。
二、JasperReport
- 前言已经说到了我的傻逼经历,被jar包依赖耗死,改用了maven。
配置maven之后,pom.xml文件如下:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sanford</groupId>
<artifactId>CloudMusic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- jasperReports报表辅助包 -->
<dependency>
<groupId>org.olap4j</groupId>
<artifactId>olap4j-xmlaserver</artifactId>
<version>1.2.0</version>
</dependency>
<!-- jasperReports报表辅助包 -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-fonts</artifactId>
<version>6.15.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-functions</artifactId>
<version>6.15.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.3.0</version>
</dependency>
<!-- groovy利用可 -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.5</version>
</dependency>
<!-- 基本数据关联 -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.4</version>
</dependency>
<!-- 文字关联 -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<!-- excel关联 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.10.1</version>
</dependency>
<!-- 图标关联 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jfree/jcommon -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jcommon</artifactId>
<version>1.0.17</version>
</dependency>
</dependencies>
</project>
本来是打算把宋体包配到本地maven仓库中,然后再在pom中写配置的,但失败了,有时间再试如何配了。
新建lib包,然后将在上文中获取到的两个jar添加到lib中,右键build path。ok,成功了,终于~可以显示中文了
另:之所以要去ireport里面找itext-asian.jar,是因为在pom里面配了之后,pom一直爆红,无果,只能另求出路,如果有大佬知道原因,麻烦评论一下。
- 调用jasperReport包导出pdf:
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
public class JasperTest {
public static Connection getConnection(String url,String user,String pwd) {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(url,user,pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) throws JRException {
String url = "jdbc:mysql://localhost:3306/cloudmusic?useUnicode=true&characterEncoding=utf8";
String user = "用户名";
String pwd = "密码";
Connection conn = getConnection(url, user, pwd);
Map<String, Object> rpt = new HashMap<String, Object>();
JasperPrint jasperPrint = JasperFillManager.fillReport("G:/iReport/report1.jasper", rpt,conn);
JasperExportManager.exportReportToPdfFile(jasperPrint, "G:/iReport/test2.pdf");
}
}
- 虽然,成功了,但是log4j总是爆红色警告。
需要配log4j的配置文件:
在resource目录下新建文件:
log4j.properties
# Global logging configuration \u5F00\u53D1\u65F6\u5019\u5EFA\u8BAE\u4F7F\u7528 debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
ok,结束。
如果不想使用maven配置的话,也可以直接在工程中加入依赖的jar包(JasperReport+iReport 所需jar包)。