解析spring boot与ireport 整合问题

pom 文件依赖

  <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/cn.lesper/iTextAsian -->
        <!-- 解决中文字体显示问题 -->
        <dependency>
            <groupId>cn.lesper</groupId>
            <artifactId>iTextAsian</artifactId>
            <version>3.0</version>
        </dependency>

controller

 @GetMapping("/preview")
    public void print(HttpServletResponse response) throws Exception {
        List<Map<String, String>> data = getMaps();
        JRDataSource dataSource = new JRBeanCollectionDataSource(data);
        File file = ResourceUtils.getFile("classpath:jaspertemplate/abc.jrxml");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/pdf");
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("test", "UTF-8") + ".pdf");

        try (OutputStream outputStream = response.getOutputStream()) {
            //编译jrxml
            JasperReport jasperReport = JasperCompileManager.compileReport(file.getPath());
            //渲染加载数据
            HashMap<String, Object> params = new HashMap<>();
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
            JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
        } catch (IOException | JRException e) {
            e.printStackTrace();
        }
    }
  private List<Map<String, String>> getMaps() {
        List<Map<String, String>> data = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map<String, String> m = new HashMap<>(3);
            m.put("id", "id" + i);
            m.put("name", "测试" + i);
            m.put("price", "price" + i);
            m.put("volTitle", "测试" + i);
            data.add(m);
        }
        return data;
    }

abc.jrxml 文件

path:src/main/resources/jaspertemplate

<?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="会计案卷目录" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="782" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20" uuid="69efba03-3736-408b-be3f-6295cabbbdfa">
	<property name="ireport.zoom" value="1.2078825000000022"/>
	<property name="ireport.x" value="48"/>
	<property name="ireport.y" value="0"/>
	<field name="volumnSeq" class="java.lang.String">
		<fieldDescription><![CDATA[volumnSeq]]></fieldDescription>
	</field>
	<field name="ajLb" class="java.lang.String">
		<fieldDescription><![CDATA[ajLb]]></fieldDescription>
	</field>
	<field name="volTitle" class="java.lang.String">
		<fieldDescription><![CDATA[volTitle]]></fieldDescription>
	</field>
	<field name="bgnDate" class="java.lang.String"/>
	<field name="fnshDate" class="java.lang.String"/>
	<field name="keepCode" class="java.lang.String">
		<fieldDescription><![CDATA[keepCode]]></fieldDescription>
	</field>
	<field name="docNmbr" class="java.lang.String">
		<fieldDescription><![CDATA[pageNmbr]]></fieldDescription>
	</field>
	<field name="remark" class="java.lang.String">
		<fieldDescription><![CDATA[remark]]></fieldDescription>
	</field>
	<field name="catalogNo" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="75" splitType="Stretch">
			<staticText>
				<reportElement x="202" y="23" width="381" height="43" uuid="6dd7ca27-e8c4-4b52-964d-69045be9bf66"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="24" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[目 录]]></text>
			</staticText>
		</band>
	</title>
	<columnHeader>
		<band height="35" splitType="Stretch">
			<staticText>
				<reportElement x="9" y="0" width="61" height="35" uuid="2f94f55b-9760-4417-95b0-99e91d5aac6f"/>
				<box>
					<pen lineWidth="1.0" lineColor="#FF3333"/>
					<topPen lineWidth="1.0" lineColor="#FF3333"/>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
					<rightPen lineWidth="0.0" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[顺序号	]]></text>
			</staticText>
			<staticText>
				<reportElement x="70" y="0" width="68" height="35" uuid="bef549c0-dc52-41f1-b291-f515a6337a7c"/>
				<box>
					<pen lineWidth="1.0" lineColor="#FF3333"/>
					<topPen lineWidth="1.0" lineColor="#FF3333"/>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
					<rightPen lineWidth="0.0" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[类别]]></text>
			</staticText>
			<staticText>
				<reportElement x="138" y="0" width="360" height="35" uuid="98ac28b1-101a-43ae-a838-1fbe3edaae7c"/>
				<box>
					<pen lineWidth="1.0" lineStyle="Solid" lineColor="#FF3333"/>
					<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#FF3333"/>
					<leftPen lineWidth="1.0"/>
					<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#FF3333"/>
					<rightPen lineWidth="0.0"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[标题]]></text>
			</staticText>
			<staticText>
				<reportElement x="498" y="0" width="100" height="35" uuid="b72ae79e-9c50-4e0f-b7fe-a0baeabc75d6"/>
				<box>
					<pen lineWidth="1.0" lineColor="#000000"/>
					<topPen lineStyle="Solid" lineColor="#FF3333"/>
					<leftPen lineStyle="Solid" lineColor="#FF3333"/>
					<bottomPen lineStyle="Solid" lineColor="#FF3333"/>
					<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[起止时间]]></text>
			</staticText>
			<staticText>
				<reportElement x="598" y="0" width="54" height="35" uuid="ef0be213-7372-4f63-85d0-11b0c68db41e"/>
				<box>
					<topPen lineWidth="1.0" lineColor="#FF3333"/>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
					<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[保管期限]]></text>
			</staticText>
			<staticText>
				<reportElement x="653" y="0" width="50" height="35" uuid="c2940de1-cdba-4a39-b2d1-537c3d245c2a"/>
				<box>
					<topPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[张数]]></text>
			</staticText>
			<staticText>
				<reportElement x="703" y="0" width="63" height="35" uuid="42adecdf-1d7c-4485-b389-88a99a16da24"/>
				<box>
					<topPen lineWidth="1.0" lineColor="#FF3333"/>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
					<rightPen lineWidth="1.0" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[备   注]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="40" splitType="Stretch">
			<textField isBlankWhenNull="true">
				<reportElement x="9" y="0" width="61" height="40" uuid="2daafeea-13c0-4ec3-919d-0409c5723171"/>
				<box>
					<pen lineWidth="1.0" lineColor="#FF3333"/>
					<topPen lineWidth="0.0"/>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
					<rightPen lineWidth="0.0"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{volumnSeq}]]></textFieldExpression>
			</textField>
			<textField isBlankWhenNull="true">
				<reportElement x="70" y="0" width="68" height="40" uuid="0ff74e34-785d-482f-a67a-72a66635bcc2"/>
				<box>
					<pen lineWidth="1.0"/>
					<topPen lineWidth="0.0" lineColor="#FF3333"/>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
					<rightPen lineWidth="0.0" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{ajLb}]]></textFieldExpression>
			</textField>
			<textField isBlankWhenNull="true">
				<reportElement x="138" y="0" width="360" height="40" uuid="88d06173-5259-4897-895d-a39434e079f2"/>
				<box>
					<topPen lineWidth="1.0" lineColor="#FF3333"/>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
					<rightPen lineWidth="1.0" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Justified" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{volTitle}]]></textFieldExpression>
			</textField>
			<textField isBlankWhenNull="true">
				<reportElement x="525" y="0" width="70" height="20" uuid="45d93afa-bd67-4f27-a3e1-c301e84ed0ea"/>
				<box>
					<bottomPen lineWidth="0.0"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{bgnDate}]]></textFieldExpression>
			</textField>
			<textField isBlankWhenNull="true">
				<reportElement x="652" y="0" width="50" height="40" uuid="3e13e790-2e18-4154-8331-ee2ab0b8f8ad"/>
				<box>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{docNmbr}]]></textFieldExpression>
			</textField>
			<textField isBlankWhenNull="true">
				<reportElement x="525" y="20" width="73" height="20" uuid="b07739c0-f452-4a53-b104-e281f40c6133"/>
				<box>
					<pen lineColor="#FF3333"/>
					<topPen lineColor="#FF3333"/>
					<leftPen lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
					<rightPen lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{fnshDate}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="498" y="0" width="30" height="20" uuid="d81412e7-1078-444a-90f4-e038ac6f30af"/>
				<box>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="0.0"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[自]]></text>
			</staticText>
			<staticText>
				<reportElement x="498" y="20" width="30" height="20" uuid="474ba566-24b3-4681-baf7-21d9647771d9"/>
				<box>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[至]]></text>
			</staticText>
			<textField isBlankWhenNull="true">
				<reportElement x="703" y="0" width="63" height="40" uuid="0cab3c98-0f59-42c4-920b-118f942bcbb9"/>
				<box>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#FF3333"/>
					<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{remark}]]></textFieldExpression>
			</textField>
			<textField isBlankWhenNull="true">
				<reportElement x="598" y="0" width="54" height="40" uuid="79eb3d06-f603-45b1-aa89-f8713daf6473"/>
				<box>
					<leftPen lineWidth="1.0" lineColor="#FF3333"/>
					<bottomPen lineWidth="1.0" lineColor="#FF3333"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{keepCode}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<pageFooter>
		<band height="45" splitType="Stretch"/>
	</pageFooter>
</jasperReport>

其他

本文不涉及有关ireport报表如何设计问题,有需要请自行查询 Y(_)Y

到此这篇关于spring boot 与 ireport 整合的文章就介绍到这了,更多相关spring boot ireport 整合内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • ireport数据表格报表的简单使用

    如果出现ireport打不开的问题正常都是因为JDK版本太高了,百度下如何简单,很容易的 首先:文件->new,选择Blank A4,Open this template 2.取个名字,选择下保存的位置,点击完成 3.此时新模板已经打开,下面我们开始介绍下模板的各部分是什么 4.设置连接驱动 第一步:选择new 第二部:选的Datebase JDBC Connection 第三步: 好了就点击save,会自动选中你刚建的数据源,下面编写SQL语句获取数据库的数据 5.编写sql语句 6.报表页面

  • iReport使用教程(示例教程)

    在使用ireport的过程中,因为各种功能都要百度,但是大家使用的例子又千差万别让人很苦恼,所以用一个简单例子贯穿的展示一下ireport的常见功能. iReport简介 说到iReport不得不先介绍Jasperreport,Jasperreport是一个报表制作程序,用户需要按照它制定的规则编写 一个XML文件,然后得到用户需要输出的格式文件.它支持输出的文件格式包括PDF,HTML,XML,XLS,CVS等等.而iReport就是一个制 作Jasperreport的XML文件的可视化开发工

  • 关于解决iReport4.1.1无法正常启动或者闪退或者JDK8不兼容的问题

    公司里面再用iReport的时候,我也安装使用了. 但是面临一个问题 我的iReport始终不能打开,困绕了老半天  但是是软件问题不方便问大佬自己研究了老大一会儿 加载之后就闪退了!!! 最后还是老大还是跟我说了需要JDK1.7才可以,我的电脑安装了两个jdk 一个是1.7的一个是1.8的   平时用的1.8  所以我开发的时候一直都是1.8  突然换成1.7的很麻烦 所以经过这两天的研究 我更改了配置文件,使得它单独强制的去使用jdk1.7来启动 1.我们打开\iReport-4.1.1\e

  • 解析spring boot与ireport 整合问题

    pom 文件依赖 <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>6.10.0</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId&

  • 深入解析Spring Boot 的SPI机制详情

    目录 简介 Java SPI实现 示例说明 创建动态接口 实现类1 实现类2 相关测试 运行结果 源码分析 Spring SPI Spring 示例 定义接口 相关实现 相关测试类 输出结果 源码分析 总结 简介 SPI(Service Provider Interface)是JDK内置的一种服务提供发现机制,可以用来启用框架扩展和替换组件,主要用于框架中开发,例如Dubbo.Spring.Common-Logging,JDBC等采用采用SPI机制,针对同一接口采用不同的实现提供给不同的用户,从

  • Spring Boot+Mybatis的整合过程

    依赖配置 结合前面的内容,这里我们要嵌入数据库的操作,这里以操作MySQL为例整合Mybatis,首先需要在原来的基础上添加以下依赖 <!-- mybatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1<

  • spring Boot与Mybatis整合优化详解

    SpringBoot官方文档http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ 关于spring-boot与mybatis整合优化方面的介绍,就是Mybatis-Spring-boot-starter的介绍: 1.取消spring-mybatis.xml配置 ①自动检测已存在的Datasource 之前,需要在spring-mybatis.xml中配置datasource的Bean,现在只需要在applicat

  • spring boot与ktor整合的实现方法

    背景 在用了一阵子 Ktor 之后,深感基于协程的方便,但是公司的主要技术栈是 SpringBoot,虽然已经整合了 Kotlin,但是如果有 Ktor 加持则会更加的方便.因此作了一番研究后,也完全可以实现这样的整合了. 建立一个 starter 首先新建一个 Kotlin 项目,在其 build.gradle 内加入对 SpringBoot 和 Ktor 的依赖,并同时加入对打为 jar 包的代码: dependencies { implementation "org.springframe

  • Spring Boot和Thymeleaf整合结合JPA实现分页效果(实例代码)

    在项目里,我需要做一个Spring Boot结合Thymeleaf前端模版,结合JPA实现分页的演示效果.做的时候发现有些问题,也查了现有网上的不少文档,发现能全栈实现的不多,所以这里我就把我的做法,全部代码和步骤贴出来供大家参考. 1 创建项目,用pom.xml引入依赖 这里将创建名为ThymeleafWithDB的Maven,在pom.xml里引入如下的依赖包. <dependencies> <dependency> <groupId>org.springframe

  • Spring Boot示例代码整合Redis详解

    目录 Redis 简介 Redis 优势 Redis与其他key-value存储有什么不同 添加Redis依赖包 配置Redis数据库连接 编写Redis操作工具类 测试 Redis 简介 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用. Redis不仅仅支持简单的key-value类型的数据

  • Spring Boot详解整合JWT教程

    目录 1.概述 2.优势所在 3.结构组成 3.1.标头(Header) 3.2.有效负载(Payload) 3.3.签名(Signature) 4.Spring boot整合JWT 导入依赖 1.概述 JWT 简称 JSON Web Token,也就是通过JSON形式作为Web应用中的令牌,用于各方之间安全地将信息作为JSON对象传输,在数据传输的过程中还可以完成数据加密.签名等相关处理. 2.优势所在 在JavaWeb阶段,经常使用session来存储,以方便用来判断用户是否操作等等.但这又

  • 简单了解Spring Boot及idea整合jsp过程解析

    一.Spring Boot简介 SpringBoot是一个框架,他的产生简化了框架的使用,所谓简化是指简化了Spring众多框架中所需的大量且繁琐的配置文件.它使用"习惯优于配置"(项目中存在大量的配置,此外还内置了一个习惯性的配置,让你无需手动进行配置)的理念让你的项目快速运行起来.使用Spring Boot很容易创建一个独立运行(运行jar,内嵌Servlet容器).准生产级别的基于Spring框架的项目,使用Spring Boot你可以不用或者只需要很少的Spring配置. Sp

  • Spring Boot与Kotlin 整合全文搜索引擎Elasticsearch的示例代码

    Elasticsearch 在全文搜索里面基本是无敌的,在大数据里面也很有建树,完全可以当nosql(本来也是nosql)使用. 这篇文章简单介绍Spring Boot使用Kotlin语言连接操作 Elasticsearch.但是不会做很详细的介绍,如果要深入了解Elasticsearch在Java/kotlin中的使用,请参考我之前编写的<Elasticsearch Java API 手册> https://gitee.com/quanke/elasticsearch-java/ 里面包含使

随机推荐