栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot集成EasyExcel

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot集成EasyExcel

     @RequestMapping("/classify")
    public void classify(@RequestParam MultipartFile file) throws IOException {
        InputStream inputStream = null;
        List 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;


public class DataListener extends AnalysisEventListener {

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

    private final List 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 getRows() {
        return rows;
    }
}
**
 * 读取Excel数据
 */
public class EasyExcelUtil {

    public static  List read(InputStream inputStream, final Class clazz) {
        if (inputStream == null) {
            throw new RuntimeException("解析出错了,文件流是null");
        }
        DataListener listener = new DataListener();
        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;

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/946513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号