Jaspersoft 报表: 基于Bean填充报表数据

概要:在实际项目中,一般使用java对象作为数据源的方式更多,这种方式会更加灵活多变。与mvc结构更为契合。上面介绍了用JDBC数据源时,用到了Fields对象。本篇主要是使用Fileds对象来匹配JavaBean对象。

第一步:创建实体对象Bean,结构如下:

package com.zzg.entity;


import java.util.Date;

import com.zzg.common.core.model.BaseEntity;

public class Column extends BaseEntity{
    private String sid;
    
    private String name;

    private String type;

    private Integer length;

    private Integer decimal;

    private String required;

    private String primary;

    private String defaultValue;

    private String comment;

    private Date createDt;

    private String createBy;

    private Date updateDt;

    private String updateBy;

    private String busTableId;

    private Integer version;

    private String state;

    private String deleteFlag;

    private String code;
    
    private String desc;

    private static final long serialVersionUID = 1L;
    
    public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc == null ? null : sid.trim();
	}

	public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid == null ? null : sid.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type == null ? null : type.trim();
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public Integer getDecimal() {
        return decimal;
    }

    public void setDecimal(Integer decimal) {
        this.decimal = decimal;
    }

    public String getRequired() {
        return required;
    }

    public void setRequired(String required) {
        this.required = required == null ? null : required.trim();
    }

    public String getPrimary() {
        return primary;
    }

    public void setPrimary(String primary) {
        this.primary = primary == null ? null : primary.trim();
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue == null ? null : defaultValue.trim();
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment == null ? null : comment.trim();
    }

    public Date getCreateDt() {
        return createDt;
    }

    public void setCreateDt(Date createDt) {
        this.createDt = createDt;
    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy == null ? null : createBy.trim();
    }

    public Date getUpdateDt() {
        return updateDt;
    }

    public void setUpdateDt(Date updateDt) {
        this.updateDt = updateDt;
    }

    public String getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy == null ? null : updateBy.trim();
    }

    public String getBusTableId() {
        return busTableId;
    }

    public void setBusTableId(String busTableId) {
        this.busTableId = busTableId == null ? null : busTableId.trim();
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state == null ? null : state.trim();
    }

    public String getDeleteFlag() {
        return deleteFlag;
    }

    public void setDeleteFlag(String deleteFlag) {
        this.deleteFlag = deleteFlag == null ? null : deleteFlag.trim();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code == null ? null : code.trim();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", sid=").append(sid);
        sb.append(", name=").append(name);
        sb.append(", type=").append(type);
        sb.append(", length=").append(length);
        sb.append(", decimal=").append(decimal);
        sb.append(", required=").append(required);
        sb.append(", primary=").append(primary);
        sb.append(", defaultValue=").append(defaultValue);
        sb.append(", comment=").append(comment);
        sb.append(", createDt=").append(createDt);
        sb.append(", createBy=").append(createBy);
        sb.append(", updateDt=").append(updateDt);
        sb.append(", updateBy=").append(updateBy);
        sb.append(", busTableId=").append(busTableId);
        sb.append(", version=").append(version);
        sb.append(", state=").append(state);
        sb.append(", deleteFlag=").append(deleteFlag);
        sb.append(", code=").append(code);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}

新建报表模板report4.jrxml

按照Column 对象新建Fileds(注意数据类型一致),我这里仅仅编辑sid 和 name 属性。

去掉多余的band,只保留Title,Detail,PageFooter。

将Filed属性拖动到 Detail 1 Band

设置页码,打开组件面板 在Composite Elements栏找到Page Number 拖入到 Page Footer 中

二、通过SpringBoot程序,使用Bean作为数据源生成报表

核心代码:

package com.zzg.report;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zzg.entity.Column;

import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

@Controller
@RequestMapping("/api/bean")
public class BeanController {
	@RequestMapping("/{reportName}")
	public void getReportByParam(@PathVariable("reportName") final String reportName, HttpServletResponse response) {
		ServletOutputStream sosRef = null;
		// bean 连接获取
		try {
			List<Column> list = new ArrayList<Column>();
			for (int i = 1; i <= 100; i++) {
				Column column = new Column();
				column.setSid("sid:" + i);
				column.setName("name is :" + i);
				list.add(column);
			}

			// 获取文件流
			ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + reportName + ".jasper");
			InputStream jasperStream = resource.getInputStream();

			sosRef = response.getOutputStream();

			JasperRunManager.runReportToPdfStream(jasperStream, sosRef, null, new JRBeanCollectionDataSource(list));
			response.setContentType("application/pdf");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				sosRef.flush();
				sosRef.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

pom.xml jar 包依赖:

<!-- 报表系统:JasperReports -->
		<dependency>
			<groupId>net.sf.jasperreports</groupId>
			<artifactId>jasperreports</artifactId>
			<version>6.6.0</version>
		</dependency>
<!-- 字体文件依赖-->
		<dependency>
			<groupId>com.zzg.report</groupId>
			<artifactId>font</artifactId>
			<version>1.0.0</version>
		</dependency>

效果展示:

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值