JasperReport+iReport匯出pdf中文失效

SanfordChern發表於2020-11-04


前言

資料庫實驗要求要列印報表,使用了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>
        <!-- 圖示關聯 -->
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jcommon</artifactId>
            <version>1.0.23</version>
            <exclusions>
                <exclusion>
                    <groupId>gnujaxp</groupId>
                    <artifactId>gnujaxp</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.0.19</version>
            <exclusions>
                <exclusion>
                    <groupId>gnujaxp</groupId>
                    <artifactId>gnujaxp</artifactId>
                </exclusion>
            </exclusions>
        </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,結束。

相關文章