ExcelUtils导出、导入

317 阅读1分钟

一 在pom文件中加入

<!--  下载excel-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.10</version>
</dependency>

二 导入ExcelUtils

import com.alibaba.excel.EasyExcel;

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

/**
 * @Description:excel工具类
 * @ClassName:ExcelUtils
 * @Author:超群
 * @Date:2022年09月17日 10:26
 */
public class ExcelUtils {


    public static<T> void exportExcel(HttpServletResponse response, Class<T> clazz, List<T> data, String fileName) throws IOException {

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String excelName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + excelName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), clazz).sheet("模板").doWrite(data);

    }
    /**
     * 导出文件时为Writer生成OutputStream
     *
     * @param fileName
     * @param response
     * @return
     */
    public static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\+", "%20");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf8");
            response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            response.setHeader("Pragma", "public");
            response.setHeader("Cache-Control", "no-store");
            response.addHeader("Cache-Control", "max-age=0");
            return response.getOutputStream();
        } catch (IOException e) {
            throw new Exception("导出excel表格失败!", e);
        }
    }
}
public R importClue(MultipartFile file, Long partnerId, Long partnerContactId) throws IOException {
    ClueExcelListener clueExcelListener = new ClueExcelListener(this, partnerId,partnerContactId, clueExcelCompenent);
    EasyExcel.read(file.getInputStream(), ClueExcelImportCmd.class, clueExcelListener).sheet().headRowNumber(2).doRead();
    return R.ok(clueExcelListener.getBatchNo());
}

三 创建实体类

import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class GoodVo {
    @ExcelProperty(value = "资产区分")
    /**
     * 资产区分
     */
    private String detaiAesTp ;
    @ExcelProperty(value = "资产code值")
    /**
     * 资产code值
     */
    private String detaiCo;
    @ExcelProperty(value = "资产型号")
    /**
     * 资产型号
     */
    private String detaiMdl;
    @ExcelProperty(value = "数量")
    /**
     * 数量
     */
    private String detaiNum;
    @ExcelProperty(value = "单位")
    /**
     * 单位
     */
    private String detaiUt;
    @ExcelProperty(value = "序列号")
    /**
     * 序列号
     */
    private String detaiSerNo;
    @ExcelProperty(value = "车牌号")
    /**
     * 车牌号
     */
    private String detaiLseNo;

    public String getDetaiAesTp() {
        return detaiAesTp;
    }

    public void setDetaiAesTp(String detaiAesTp) {
        this.detaiAesTp = detaiAesTp;
    }

    public String getDetaiCo() {
        return detaiCo;
    }

    public void setDetaiCo(String detaiCo) {
        this.detaiCo = detaiCo;
    }

    public String getDetaiMdl() {
        return detaiMdl;
    }

    public void setDetaiMdl(String detaiMdl) {
        this.detaiMdl = detaiMdl;
    }

    public String getDetaiNum() {
        return detaiNum;
    }

    public void setDetaiNum(String detaiNum) {
        this.detaiNum = detaiNum;
    }

    public String getDetaiUt() {
        return detaiUt;
    }

    public void setDetaiUt(String detaiUt) {
        this.detaiUt = detaiUt;
    }

    public String getDetaiSerNo() {
        return detaiSerNo;
    }

    public void setDetaiSerNo(String detaiSerNo) {
        this.detaiSerNo = detaiSerNo;
    }

    public String getDetaiLseNo() {
        return detaiLseNo;
    }

    public void setDetaiLseNo(String detaiLseNo) {
        this.detaiLseNo = detaiLseNo;
    }
}

四 控制层代码

//获取请求参数  
HaecMap haecMap = requestData.getParam();
//查询商品数据
HaecVO goodsList=iobinApplicaService.selectGoodList(haecMap);
List<GoodVo> goodVos = new ArrayList<>();
goodsList.getTable().forEach(haecMap1 -> {
    goodVos.add(transGoodVo(haecMap1));
});
String time= DateUtils.getTime();
String fileName=time+" "+getUsername()+"商品物品表";
ExcelUtils.exportExcel(response,GoodVo.class,goodVos,fileName);