首页 > 其他分享 >使用EasyExcel的AnalysisEventListener读取EXCEL导入数据

使用EasyExcel的AnalysisEventListener读取EXCEL导入数据

时间:2024-07-05 10:33:16浏览次数:16  
标签:AnalysisEventListener return String EasyExcel EXCEL private import new public

1、实体对象VO

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
 
@Data
public class PrizeLogImportExcelVO {
    @ExcelProperty("订单编号")
    private String prizeSn;
    @ExcelProperty("快递公司")
    private String expressName;
    @ExcelProperty("快递单号")
    private String expressSn;
    @ExcelProperty("快递编码")
    private String expressCode;
    @ExcelProperty("快递ID")
    private Integer expressId;
}

   2、接口类

public interface IJmPrizeLogService {
    /**
     * 导入
     *
     * @param list
     */
    void importExcel(List<PrizeLogImportExcelVO> list);
}

  3、接口实现类

@Service
public class JmPrizeLogServiceImpl extends ServiceImpl<JmPrizeLogMapper, JmPrizeLog> implements IJmPrizeLogService {
    public static List<String> errorPrizeSns = new ArrayList<>();
    @Resource
    private JmPrizeLogMapper prizeLogMapper;
    @Resource
    private JmExpressMapper expressMapper;
    private static final ExecutorService POOL = Executors.newCachedThreadPool();
 
    @Override
    public void importExcel(List<PrizeLogImportExcelVO> list) {
        errorPrizeSns.clear();
        if (CollectionUtil.isEmpty(list)) {
            errorPrizeSns.add("excel中无数据,无法导入发货!");
            return;
        }
        List<PrizeLogImportExcelVO> result = new ArrayList<>();
        for (PrizeLogImportExcelVO importExcelVO : list) {
            String prizeSn = importExcelVO.getPrizeSn();
            if (StringUtils.isEmpty(prizeSn)) {
                continue;
            }
            if (StringUtils.isBlank(importExcelVO.getExpressName())) {
                errorPrizeSns.add(prizeSn + " 快递公司不能为空!");
                continue;
            }
            if (StringUtils.isBlank(importExcelVO.getExpressSn())) {
                errorPrizeSns.add(prizeSn + " 快递单号不能为空!");
                continue;
            }
            QueryWrapper<JmExpress> expressQueryWrapper = new QueryWrapper<>();
            expressQueryWrapper.eq("name", importExcelVO.getExpressName());
            JmExpress jmExpress = expressMapper.selectOne(expressQueryWrapper);
            if (!ObjectUtil.isNotEmpty(jmExpress)) {
                errorPrizeSns.add(prizeSn + " 快递公司有误!");
                continue;
            } else {
                importExcelVO.setExpressCode(jmExpress.getCode());
                importExcelVO.setExpressId(jmExpress.getId().intValue());
            }
            result.add(importExcelVO);
        }
        if (CollectionUtil.isEmpty(errorPrizeSns)) {
            POOL.execute(() -> {
                prizeLogMapper.batchExpressInfo(result);
            });
        }
    }
}

  Mapper接口

public interface JmPrizeLogMapper extends BaseMapper<JmPrizeLog> {
    /**
     * 批量导入快递信息
     *
     * @param list
     */
    @Update("<script><foreach collection='list' item='o' index='index' separator=';' >UPDATE `jm_prize_log` SET express_name =#{o.expressName}, express_sn = #{o.expressSn}, express_code = #{o.expressCode}, express_id = #{o.expressId} WHERE prize_sn = #{o.prizeSn}</foreach></script>")
    void batchExpressInfo(@Param("list") List<PrizeLogImportExcelVO> list);
}

  5、定义一个ExcelHander工具类继承AnalysisEventListener 最重要的是重写invoke方法,去执行读EXCEL逻辑。

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jumi.microservice.sale.entity.vo.PrizeLogImportExcelVO;
import com.jumi.microservice.sale.service.IJmPrizeLogService;
 
import java.util.ArrayList;
import java.util.List;
 
 
public class PrizeLogImportListener extends AnalysisEventListener<PrizeLogImportExcelVO> {
    private IJmPrizeLogService prizeLogService;
    private List<PrizeLogImportExcelVO> list = new ArrayList<>();
 
    @Override
    public void invoke(PrizeLogImportExcelVO data, AnalysisContext context) {
        list.add(data);
    }
 
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        prizeLogService.importExcel(list);
    }
 
    public PrizeLogImportListener(IJmPrizeLogService prizeLogService) {
        this.prizeLogService = prizeLogService;
    }
 
    public List<PrizeLogImportExcelVO> getList() {
        return list;
    }
}

  6、调用逻辑

    @Resource
    private IJmPrizeLogService prizeLogService;
    
//    @PostMapping("/import/excel")
//    @ApiOperation("导入excel")
//    public ResponseResult<List<String>> importExcel(@RequestParam(value = "multipartFile") MultipartFile request) throws IOException {
//        PrizeLogImportListener listener = new PrizeLogImportListener(prizeLogService);
//        EasyExcel.read(request.getInputStream(), PrizeLogImportExcelVO.class, listener).sheet().doRead();
//        return ResponseResult.success(JmPrizeLogServiceImpl.errorPrizeSns);
//    }
 
    @PostMapping("/import/excel")
    @ApiOperation("导入excel")
    public ResponseResult<List<String>> importExcel(@RequestParam(value = "multipartFile") MultipartFile file) throws IOException {
        if (file == null || file.isEmpty() || ObjectUtils.isEmpty(file.getOriginalFilename())) {
            throw new BaseException(500, "文件不能为空");
        }
        String fileName = file.getOriginalFilename();
        if (!(fileName.endsWith(".xlsx") || fileName.endsWith(".xls") || fileName.endsWith(".csv"))) {
            throw new BaseException(500, "文件类型错误,只支持:xlsx、xls、csv");
        }
        try {
            InputStream inputStream = file.getInputStream();
            if (fileName.endsWith(".csv")) {
                inputStream = CsvToXlsxUtil.csvStream2xlsxStream(file.getInputStream(), fileName);
            }
            PrizeLogImportListener listener = new PrizeLogImportListener(prizeLogService);
            EasyExcel.read(inputStream, PrizeLogImportExcelVO.class, listener).sheet().doRead();
        } catch (ExcelAnalysisException | ExcelCommonException | IOException e) {
            System.out.println(e);
            throw new BaseException(500, "文件异常,请检查确认");
        }
        return ResponseResult.success(JmPrizeLogServiceImpl.errorPrizeSns);
    }

  7、CsvToXlsxUtil工具类

import cn.hutool.core.util.ObjectUtil;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jumpmind.symmetric.csv.CsvReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
 
/**
 * Csv 转 Xlsx 工具类
 */
public class CsvToXlsxUtil {
    private static final Logger log = LoggerFactory.getLogger(CsvToXlsxUtil.class);
    /**
     * CSV常用分隔符,如需动态扩展设置成配置项
     */
    private static final char[] DELIMITERS = {
            ',',
            ';',
            '\001',
            ' ',
            '\t',
            '|',
            '#',
            '&'
    };
 
    /**
     * 读取CSV文件并写入到XLSX文件中,默认编码
     *
     * @param csvFileAddress 文件地址
     */
    public static String csvToXlsx(String csvFileAddress) {
        return csvToXlsx(csvFileAddress, "UTF-8");
    }
 
    /**
     * @param inputStream 输入流
     */
    public static InputStream csv2xlsx(InputStream inputStream, String fileName) {
        return csvStream2xlsxStream(inputStream, fileName);
    }
 
    /**
     * 读取CSV文件并写入到XLSX文件中,指定CSV文件编码
     *
     * @param csvFileAddress 文件地址
     * @param charset        编码
     */
    public static String csvToXlsx(String csvFileAddress, String charset) {
        String xlsxFileAddress = "";
        FileOutputStream fileOutputStream = null;
        try {
            char delimiter = getDelimiter(csvFileAddress);
            //xlsx file address
            xlsxFileAddress = csvFileAddress.replace("csv", "xlsx");
            XSSFWorkbook workBook = new XSSFWorkbook();
            XSSFSheet sheet = workBook.createSheet(getSheetName(csvFileAddress));
            int rowNum = -1;
            CsvReader csvReader = new CsvReader(csvFileAddress, delimiter, Charset.forName(charset));
            while (csvReader.readRecord()) {
                rowNum++;
                XSSFRow currentRow = sheet.createRow(rowNum);
                for (int i = 0; i < csvReader.getColumnCount(); i++) {
                    currentRow.createCell(i).setCellValue(csvReader.get(i));
                }
            }
            fileOutputStream = new FileOutputStream(xlsxFileAddress);
            workBook.write(fileOutputStream);
            return getFileName(xlsxFileAddress);
        } catch (Exception e) {
            log.error("CsvToXlsxUtil exception :", e);
        } finally {
            try {
                assert fileOutputStream != null;
                fileOutputStream.close();
            } catch (IOException e) {
                log.error("CsvToXlsxUtil close FileOutputStream exception :", e);
            }
        }
        return getFileName(xlsxFileAddress);
    }
 
    /**
     * @param inputStream 输入流
     */
    public static InputStream csvStream2xlsxStream(InputStream inputStream, String fileName) {
        FileOutputStream fileOutputStream = null;
        try {
            fileName = fileName.replace(".csv", ".xlsx");
            XSSFWorkbook workBook = new XSSFWorkbook();
            XSSFSheet sheet = workBook.createSheet("sheet1");
            int rowNum = -1;
            CsvReader csvReader = new CsvReader(inputStream, StandardCharsets.UTF_8);
            while (csvReader.readRecord()) {
                rowNum++;
                XSSFRow currentRow = sheet.createRow(rowNum);
                for (int i = 0; i < csvReader.getColumnCount(); i++) {
                    currentRow.createCell(i).setCellValue(csvReader.get(i));
                }
            }
            File file = new File("/" + fileName);
            fileOutputStream = new FileOutputStream(file);
            workBook.write(fileOutputStream);
            InputStream input = new FileInputStream(file);
            file.delete();
            return input;
        } catch (Exception e) {
            log.error("CsvToXlsxUtil exception :", e);
        } finally {
            try {
                if (ObjectUtil.isNotNull(fileOutputStream)) {
                    assert fileOutputStream != null;
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                log.error("CsvToXlsxUtil close FileOutputStream exception :", e);
            }
        }
        return null;
    }
 
    /**
     * 设置excel文件的sheet名称
     * 获取CSV文件名作为Excel文件的sheet名称
     *
     * @param path 资源路径
     */
    private static String getSheetName(String path) {
        try {
            String[] file = getFileName(path).split("\\.");
            return file[0];
        } catch (Exception e) {
            log.error("CsvToXlsxUtil get sheet name exception : ", e);
            return "Sheet";
        }
    }
 
    /**
     * 根据资源路径切割获取文件名
     *
     * @param path 资源路径
     */
    private static String getFileName(String path) {
        String[] paths = path.contains("\\") ? path.split("\\\\") : path.split("/");
        return paths[paths.length - 1];
    }
 
    /**
     * 常用CSV分隔符数组遍历资源第一行,分隔的字段数多的为资源分隔符
     * 异常情况下默认用’,‘作为分隔符
     *
     * @param path 资源路径
     */
    private static char getDelimiter(String path) {
        BufferedReader br = null;
        char delimiter = ',';
        try {
            br = new BufferedReader(new FileReader(path));
            String line = br.readLine();
            CsvReader csvReader;
            int columCount = 0;
            for (char delimiterTest : DELIMITERS) {
                csvReader = new CsvReader(getStringStream(line), delimiterTest, StandardCharsets.UTF_8);
                if (csvReader.readRecord()) {
                    int newColumnCount = csvReader.getColumnCount();
                    if (newColumnCount > columCount) {
                        columCount = newColumnCount;
                        delimiter = delimiterTest;
                    }
                }
            }
        } catch (Exception e) {
            log.error("CsvToXlsxUtil get delimiter exception :", e);
        } finally {
            try {
                assert br != null;
                br.close();
            } catch (IOException e) {
                log.error("CsvToXlsxUtil get delimiter close BufferedReader exception :", e);
            }
        }
        return delimiter;
    }
 
    /**
     * 字符串转输入流
     * 把CSV文件第一行数据转成输入流
     *
     * @param sInputString 字符串
     */
    private static InputStream getStringStream(String sInputString) {
        if (null != sInputString && !"".equals(sInputString)) {
            try {
                return new ByteArrayInputStream(sInputString.getBytes());
            } catch (Exception e) {
                log.error("CsvToXlsxUtil get StringStream exception :", e);
            }
        }
        return null;
    }
}

  

标签:AnalysisEventListener,return,String,EasyExcel,EXCEL,private,import,new,public
From: https://www.cnblogs.com/xianz666/p/18285257

相关文章

  • 艾宾浩斯遗忘曲线复习计划表Excel下载
    艾宾浩斯遗忘曲线复习计划表Excel下载 改造自贵乎大神的Excel:艾宾浩斯遗忘曲线怎么用?链接:https://pan.baidu.com/s/1rqQLOPXAxTxDs_Tk9fIN5A提取码:u53x如果失效了,记得提醒我 日期:是从今天2020/11/02计划到2025/05/26用法:不要修改或删除任何日期(任何一行都不要删除掉......
  • 树形结构导出excel表格
    树形结构导出excel表格原本是想做成这样但是没学会,最后做成这样直接看代码publicStringexcelDowmload(DormCheckStatParamDtoparam){StringresultUrl=null;param.setSearchAll(SEARCH_ALL_NO);try{DormCheckResult......
  • 06-Excel初阶操作-学习笔记
    SUMIF和SUMIFS单(多)条件求和函数函数格式参数说明SUMTIF(参数1,参数2,参数3)参数1:区域参数2:符合条件参数3:求和区域SUMIFS(参数1,参数2,参数3,参数4,……)参数1:求和区域参数2:区域参数3:符合条件参数4:区域……基础应用SUMTIF(参数1,参数2,参数3)......
  • 钉钉应用开发-Python操作钉钉文档(excel版)
    钉钉应用开发-Python操作钉钉文档一:服务端SDK下载服务端SDK下载-钉钉开放平台(dingtalk.com)pip3installalibabacloud_dingtalk二:钉钉开放平台开发者后台(dingtalk.com)基础概念-钉钉开放平台(dingtalk.com)2.1:创建应用2.2:获取应用基本信息2.3:权限申请,获取......
  • EXCEL中20个数据处理类函数公式应用实例
    在Excel中,数据处理类函数是进行数据分析和报告制作的重要工具。以下列举了另外20个数据处理类函数及其应用实例,这些函数涵盖了一系列高级的数据处理需求,包括统计分析、财务计算、工程计算以及更复杂的文本和日期时间操作。数据统计与分析STDEV.S函数:用于计算样本标准差。例......
  • excel基本操作
    1.设置保护/锁定工作表和工作薄设置保护表:打开excel--->打开审阅--->点击允许编辑区域  其他区域不能进行输入 设置保护工作薄:sheet1 sheet2就是工作薄2.填充柄:鼠标左键下拉 3.利用自定义列表填充  文件-》选项-》高级-》编辑自定义列表4.填充柄:鼠标右键下拉......
  • 像学Excel 一样学 Pandas系列-创建数据分析维度
    嗨,小伙伴们。又到喜闻乐见的Python数据分析王牌库Pandas的学习时间。按照数据分析处理过程,这次轮到了新增维度的部分了。老样子,我们先来回忆一下,一个完整数据分析的过程,包含哪些部分内容。其中,Pandas的基础信息导入、数据导入和数据整理、数据探索和清洗已经在前几篇文章......
  • 学习笔记485—Excel技巧:一键将文本数字转换为数值
    Excel技巧:一键将文本数字转换为数值在使用Excel进行数据处理时,经常会遇到数据格式不匹配的问题。特别是当从外部导入数据或手动输入数据时,数字可能会被误识别为文本格式,这在进行数据计算和分析时会带来诸多不便。幸运的是,Excel提供了一些便捷的方法,可以帮助我们一键将文本转换为......
  • 基于POI的Excel导出
    基于POI的Excel导出1、后端依赖1<dependencies>2<dependency>3<groupId>org.apache.poi</groupId>4<artifactId>poi</artifactId>5<version>5.2.3</version>6</dependency>......
  • EasyExcel 填充+写入
    使用EasyExcel导出Excel时,有时会遇到如下情况:既要根据模板填充某些sheet又要根据业务写入某些sheetEasyExcel官方没有提供这样的示例,经过自己的研究和实验,得到了如下步骤:定义导出文件名StringfileName="测试.xlsx";获取模板文件InputStreamtemplateFile......