easyexcel同步下载简单使用

news/2024/7/21 4:25:54 标签: easyexcel, excel, java

1.加pom

        <!-- 阿里EasyExcel -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>excel>easyexcel</artifactId>
            <version>3.1.1</version>
        </dependency>

2. 写工具类

java">package com.fn.health.common.utils;

import com.fn.health.common.domain.DownloadResultDomain;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

public class WebUtil {

	/**
	 * 下载文件需要的必要设置
	 *
	 * @param res
	 * @param downloadResultDomain
	 */
	public static void downloadFile(HttpServletResponse res, DownloadResultDomain downloadResultDomain) {
		OutputStream out;
		InputStream inputStream;
		try {
			res.setContentType("application/octet-stream");
			/** 读取服务器端模板文件*/
			res.setHeader("Content-Disposition",
				"attachment;fileName=" + URLEncoder.encode(downloadResultDomain.getFileName(), "utf-8").replaceAll("\\+", "%20"));
			inputStream = downloadResultDomain.getInputStream();
			out = res.getOutputStream();
			int b = 0;
			byte[] buffer = new byte[1024];
			while ((b = inputStream.read(buffer)) != -1) {
				// 4.写到输出流(out)中
				out.write(buffer, 0, b);
			}
			inputStream.close();
			if (out != null) {
				out.flush();
				out.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void showImage(HttpServletResponse res,InputStream inputStream){
		OutputStream out;
		try {
			res.setContentType("image/png");
			out = res.getOutputStream();
			int b = 0;
			byte[] buffer = new byte[1024];
			while ((b = inputStream.read(buffer)) != -1) {
				// 4.写到输出流(out)中
				out.write(buffer, 0, b);
			}
			inputStream.close();
			if (out != null) {
				out.flush();
				out.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

java">package com.fn.health.common.domain;

import com.fn.health.common.bean.UploadDataBean;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;


@Data
public class UploadDataResultDomain {
    /**
     * 错误数量
     */
    private int errorCount = 0;
    /**
     * 完成数量
     */
    private int successCount = 0;
    /**
     上传错误数据
     */
    private List<UploadDataBean> uploadErrorData = new ArrayList<>();
    /**
     * 导入的批次号
     */
    private String importNo;

    public void errorAdd() {
        errorCount++;
    }

    public void successAdd() {
        successCount++;
    }

}

java">package com.fn.health.common.domain;

import lombok.Data;

import java.io.InputStream;
import java.io.Serializable;

/**
 * @author owen.chen
 * @date 2020/9/27 9:18
 */
@Data
public class DownloadResultDomain implements Serializable {

	public DownloadResultDomain(String fileName, InputStream inputStream) {
		this.fileName = fileName;
		this.inputStream = inputStream;
	}

	private String fileName;
	private InputStream inputStream;

}

java">package com.fn.health.common.bean;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;

public abstract class UploadDataBean {

	/**
	 * 行号
	 */
	@ExcelIgnore
	protected Long excelNumberNo;
	/**
	 * 记录是否空行
	 */
	@ExcelIgnore
	protected Boolean isBlank;
	/**
	 * 判定记录是否有效
	 */
	@ExcelIgnore
	protected Boolean isValid;
    /**
     * 上传的数据一次
     */
	@ExcelProperty(value = "错误详情")
    protected String uploadErrorMsg;

	/// getter,setter

	public Long getExcelNumberNo() {
		return excelNumberNo;
	}

	public void setExcelNumberNo(Long excelNumberNo) {
		this.excelNumberNo = excelNumberNo;
	}

	public Boolean getBlank() {
		return isBlank;
	}

	public void setBlank(Boolean blank) {
		isBlank = blank;
	}

	public Boolean getValid() {
		return isValid;
	}

	public void setValid(Boolean valid) {
		isValid = valid;
	}

	public String getUploadErrorMsg() {
        return uploadErrorMsg;
    }

    public void setUploadErrorMsg(String uploadErrorMsg) {
        this.uploadErrorMsg = uploadErrorMsg;
    }

}

3.写个例子测试

java">List<Map<String, Object>> replyList = rwsPatientCaseService.queryRecruitReplyList(recruitId, doctorId, status);
            //设置分页数据
            RwsPatientCaseHandler.download(replyList,response);
java">package com.fn.health.download.rwsPatientCase;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.fn.health.common.domain.DownloadResultDomain;
import com.fn.health.common.utils.WebUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class RwsPatientCaseHandler {

    public  static void download(List<Map<String, Object>> replyList, HttpServletResponse response){
        List<RwsPatientCaseExcel> excelList=RwsPatientCaseExcel.ToList(replyList);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, RwsPatientCaseExcel.class).sheet("导出数据").doWrite(excelList);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(out.toByteArray());
        DownloadResultDomain downloadResultDomain = new DownloadResultDomain("患者数据.xlsx", byteArrayInputStream);
        WebUtil.downloadFile(response, downloadResultDomain);
    }
}


http://www.niftyadmin.cn/n/5043702.html

相关文章

Linux Static Key原理与应用

文章目录 背景1. static-key的使用方法1.1. static-key定义1.2 初始化1.3 条件判断1.4 修改判断条件 2、示例代码参考链接 背景 内核中有很多判断条件在正常情况下的结果都是固定的&#xff0c;除非极其罕见的场景才会改变&#xff0c;通常单个的这种判断的代价很低可以忽略&a…

在win10上格式化Linux启动盘

U盘制作Linux启动盘后无法在win10上格式化恢复原来的大小&#xff0c;可采取下面的方法&#xff1a; 在win10上进行操作:打开cmd输入:diskpart enter健会弹出一个新的对话框。 在新的对话框中输入:list disk&#xff0c;会列出计算机的磁盘列表。 List item选择u盘的序号:sele…

保研CS/软件工程/通信问题汇总

机器学习 1.TP、TN、FP、FN、F1 2.机器学习和深度学习的区别和联系 模型复杂性&#xff1a;深度学习是机器学习的一个子领域&#xff0c;其主要区别在于使用深层的神经网络模型。深度学习模型通常包含多个隐层&#xff0c;可以学习更加复杂的特征表示&#xff0c;因此在某些任…

搭建ELK+Filebead+zookeeper+kafka实验(详细版)

一、ELKFilebeadzookeeperkafka架构 第一层&#xff1a;数据采集层&#xff08;Filebeat&#xff09; 数据采集层位于最左边的业务服务集群上&#xff0c;在每个业务服务器上面安装了filebead做日志收集&#xff0c;然后把采集到的原始日志发送到kafkazookeeper集群上。 第二…

Jackson 中的 @JsonProperty 和 @JsonAlias 的区别

1. 背景 在Spring项目中&#xff0c;使用到Jackson用来做Json对象的序列化和反序列化。这里就涉及到了自定义字段名称&#xff0c;通常可以使用 JsonProperty 和 JsonAlias &#xff0c;那这两者的区别是什么呢&#xff1f; 简单的说&#xff0c;JsonProperty 支持序列化和反…

2023华为杯数学建模竞赛E题

一、前言 颅内出血&#xff08;ICH&#xff09;是由多种原因引起的颅腔内出血性疾病&#xff0c;既包括自发性出血&#xff0c;又包括创伤导致的继发性出血&#xff0c;诊断与治疗涉及神经外科、神经内科、重症医学科、康复科等多个学科&#xff0c;是临床医师面临的重要挑战。…

2023-09-22 精神分析-DQ-行为后果-分析

摘要&#xff1a; 纸牌屋中的弗兰克在往猎食者之路攀登的过程中曾感慨, 越往上爬尔虞我诈越激烈, 斗争越是凶狠。身为技术开发者, 虽然觉得浙西事情很恶心, 但是必须要对一些潜在的影响事业的事情引起警觉, 以尽量消灭在萌芽之中. 本文分析DQ在我的对事情的逼迫下展开的反击,…

QT:使用堆叠窗口、标签、下拉条

widget.h #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QStackedWidget> //堆叠窗口 #include <QComboBox> //下拉条 #include <QLabel> //标签class Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *p…