接着上一次所说的,没看过的可跳转
Jasper(1)——入门
第一阶段:初步导出PDF
Jasper报表设计
点击 Static Text,并拖动到 title 位置
然后双击此框,根据需要修改,可看下图
修改好后可以调一下蓝线(各个band的高度)或者如图所示
或者如下图修改
接着Page Header我随便弄了点中文,跟title差不多
在弄Column Header和Detail部分之前,先做点东西~
outline视图,右键field -> Create Field
你会看到如下图的亚子
比如我这个field是id,类型为Integer,看String是不符的,因此如下图操作
这样,id列名就创好啦~接下来如法炮制,创建name和school列名,两者都是String类型
接下来,我们把它们拖动到Detail部分,然后慢慢调整弄整齐
PS:拖到Detail的时候,Column Header也对应有一个框(Static Text)!
其实,看到图中,是否会有问“$F{id}”是啥意思?
其实Detail的框是Text Field,双击一看,如图
Column Footer我就随便写了哈,不展示了
Page Footer一般是写页码之类的,我就用个函数,如下图所示
Summary部分我这边加一个入参,这个入参创建参考上面的field即可
这样一个报表就完成啦~可以预先浏览
这里就是对应该xml的源码
编译该文件
这样设计报表部分完成!
Java代码导出部分
本次我用Springboot来进行pdf导出
文件目录大概是这样
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>org.example</groupId>
<artifactId>jasperdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>5.6.1</version>
<exclusions>
<exclusion>
<groupId>org.olap4j</groupId>
<artifactId>olap4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.olap4j</groupId>
<artifactId>olap4j</artifactId>
<version>0.9.7.309-JS-3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jfree/jcommon -->
<dependency>
<groupId>jfree</groupId>
<artifactId>jcommon</artifactId>
<version>1.0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jfree/jfreechart -->
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
本次我将用模拟数据来演示,创建一个Student类
package com.copa.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* @author copa
* @createDate 2021-07-25 21:51
* @function
*/
@Data
@AllArgsConstructor
public class Student {
private Integer id;
private String name;
private String school;
}
controller
package com.copa.controller;
import com.copa.domain.Student;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* @author copa
* @createDate 2021-07-25 21:02
* @function 导出pdf
*/
@RestController
public class TestController {
@RequestMapping(value = "/pdf1")
public void pdf1(Map<String, Object> parameters,
HttpServletResponse response) throws IOException {
ServletOutputStream os = response.getOutputStream();
try {
ArrayList<Student> list = new ArrayList<>();
for (int i = 1; i <= 40; i++) {
list.add(new Student(i, "copa" + i, i <= 20 ? "一中" : "二中"));
}
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
parameters = parameters == null ? new HashMap<>() : parameters;
parameters.put("summary", "我是summary~");
ClassPathResource resource = new ClassPathResource("jasper/demo1.jasper");
response.setContentType("application/pdf");
InputStream jasperStream = resource.getInputStream();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperStream, parameters, dataSource);
JasperExportManager.exportReportToPdfStream(jasperPrint, os);
} catch (JRException e) {
e.printStackTrace();
} finally {
os.flush();
}
}
}
那么,启动项目,查看效果
http://localhost:8080/pdf1
看起来…好像没边框很别扭,那么我们返回去加边框
重新编译,覆盖文件,再次尝试~
这样,边框就出来了~但是,有没有看到有些内容没显示的o(╥﹏╥)o
这是…中文的问题…因为Jasper它没考虑到中文,查看它的源码可知
第二阶段:解决中文问题
首先,从网上找字体,比如下图
然后这里添加字体
这样就添加好字体了,然后给显示中文的都换上这个字体
然后编译并覆盖
在java代码这一块,创建下图几个文件
ttf文件是你在报表设计时用到的字体文件
font.xml
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="是微软雅黑吖">
<normal>jasper/fonts/msyh.ttf</normal>
<bold>jasper/fonts/msyh.ttf</bold>
<italic>jasper/fonts/msyh.ttf</italic>
<boldItalic>jasper/fonts/msyh.ttf</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.html">'是微软雅黑吖', Arial, Helvetica, sans-serif</export>
<export key="net.sf.jasperreports.xhtml">'是微软雅黑吖', Arial, Helvetica, sans-serif</export>
</exportFonts>
</fontFamily>
</fontFamilies>
jasperreports_extension.properties
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.dejavu=jasper/fonts/fonts.xml
添加完成后,再次运行
http://localhost:8080/pdf1 这样就成功啦~
底部也对应显示~
这样导出PDF报表就完成啦~
O(∩_∩)O~