springboot集成EasyExcel

     @RequestMapping("/classify")
    public void classify(@RequestParam MultipartFile file) throws IOException {
        InputStream inputStream = null;
        List<ClassifyExcelVO> classifyVOS = new ArrayList<>();
        try {
            inputStream = file.getInputStream();
            classifyVOS = EasyExcelUtil.read(inputStream, ClassifyExcelVO.class);
            LOGGER.info("解析数据:"+classifyVOS);
        } catch (IOException e) {
            log.error("文件错误!");
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }
package com.oppo.ipd.pdm.standard.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * excel解析过程监听器
 *
 * @author YJ
 */
public class DataListener<T> extends AnalysisEventListener<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(DataListener.class);

    private final List<T> rows = new ArrayList<>();

    @Override
    public void invoke(T t, AnalysisContext analysisContext) {
        rows.add(t);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        LOGGER.info("解析完成!读取{}行", rows.size());
    }

    public List<T> getRows() {
        return rows;
    }
}
**
 * 读取Excel数据
 */
public class EasyExcelUtil {

    public static <T> List<T> read(InputStream inputStream, final Class<?> clazz) {
        if (inputStream == null) {
            throw new RuntimeException("解析出错了,文件流是null");
        }
        DataListener<T> listener = new DataListener<T>();
        EasyExcel.read(inputStream,clazz,listener).sheet().doRead();
        return listener.getRows();
    }
}
@Data
public class ImportFileNewsVO implements Serializable {

    @ExcelProperty(value = "文件类型",index = 0)
    private String fileType;

    @ExcelProperty(value = "名称",index = 1)
    private String name;

    @ExcelProperty(value = "简介",index = 2)
    private String introduction;

    @ExcelProperty(value = "文件类容",index = 3)
    private String files;

    @ExcelProperty(value = "归档位置",index = 4)
    private String location;

    @ExcelProperty(value = "适用产品类型",index = 6)
    private String applyProductType;

    @ExcelProperty(value = "是否限制限制权限",index = 7)
    private String isLimitDownloadAuth;

    @ExcelProperty(value = "下载人员名单",index = 8)
    private String downloadPersonnelList;

    @ExcelProperty(value = "版本说明",index = 9)
    private String versionInstruction;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值