1.jaspersoft studio工具下载地址
2.工具使用方法查看以下链接
3.将工具编译后的.jasper文件放到SpringBoot项目的resources/templates下
4.引入需要的jar
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.18.1</version>
<exclusions>
<exclusion>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
<exclusion>
<artifactId>commons-beanutils</artifactId>
<groupId>commons-beanutils</groupId>
</exclusion>
<exclusion>
<artifactId>commons-collections</artifactId>
<groupId>commons-collections</groupId>
</exclusion>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.11</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-fonts</artifactId>
<version>6.18.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-pdfa</artifactId>
<version>5.5.0</version>
</dependency>
ps:jasperreports 、jasperreports-fonts这两jar的版本取决于使用的jaspersoft studio工具的版本(一定要一致,否则可能出现类型转换异常)
5.pom文件配置
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.jasper</exclude>
<exclude>**/*.jrxml</exclude>
</excludes>
</resource>
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.jasper</include>
<include>**/*.jrxml</include>
</includes>
</resource>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<delimiters>
<delimiter>$</delimiter>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
<outputDirectory>${basedir}/target/classes/</outputDirectory>
<nonFilteredFileExtensions>
<!-- 需要过滤掉不需要编码的文件,不对其进行统一编码 -->
<nonFilteredFileExtension>woff</nonFilteredFileExtension>
<nonFilteredFileExtension>woff2</nonFilteredFileExtension>
<nonFilteredFileExtension>eot</nonFilteredFileExtension>
<nonFilteredFileExtension>ttf</nonFilteredFileExtension>
<nonFilteredFileExtension>svg</nonFilteredFileExtension>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
<nonFilteredFileExtension>docx</nonFilteredFileExtension>
<nonFilteredFileExtension>doc</nonFilteredFileExtension>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>jasper</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
ps:配置要有,否则可能会出现io异常
6.服务代码,我这里使用的是map传参的方式
//引入jasper文件
ClassPathResource resource = new ClassPathResource("templates/collate.jasper");
try {
//文件输入流的两种获取方式
// FileInputStream fis = new FileInputStream(resource.getFile());
InputStream inputStream = resource.getInputStream();
Map<String, Object> map = new HashMap<>();
map.put("userName","zhangsan");
map.put("pass","123456")
/**查看底层代码发现JasperFillManager.fillReport(inputStream, map,new JREmptyDataSource())
*中有调用(JasperReport) JRLoader.loadObject(inputStream)所以在这里我注掉了
*如果要使用(JasperReport) JRLoader.loadObject(inputStream)该方法
*fillReport方法的参数改为JasperFillManager.fillReport(jasperReport, map,new JREmptyDataSource())
*/
// JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
//将数据源传递collate.jasper进行数据拼装
//fillReport接口就是处理数据和.jasper文件交互的方法
JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, map,new JREmptyDataSource());
//调用打印方法
JasperPrintManager.printReport(jasperPrint,true);
} catch (Exception e) {
e.printStackTrace();
}
参数解释:
JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, map,new JREmptyDataSource());
//参数1输入流,即读取的文件
//参数2数据map集合
//参数3数据源,我这里使用的是map传参所有数据源给的空数据源对象,必须要有数据源对象
//也可以使用数据源的方式调用,map给个空的hasmap即可,数据源即jdbc连接
7.项目设置
如果是在linux系统运行可以使用 nohup java -Djava.awt.headless=false -jar xxx.jar >catalina.out 2>&1 &