乐趣区

关于springboot:Springboot-使用easyexcel读写Excel

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.3.2</version>
        </dependency>

简略粗犷无效,间接 ImportExcel.class 对象全副 String 类型,读取到 data 后,按需本人解决

public String readExcel(@RequestPart("file") MultipartFile file) {
        try {EasyExcel.read(file.getInputStream(), ImportExcel.class, new ReadListener<ImportExcel>() {
                public static final int BATCH_COUNT = 10;
                private List<ImportExcel> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

                @Override
                public void invoke(ImportExcel data, AnalysisContext context) {
                    // todo 全副为 String 类型的按需转换为本人最终想要的类型
                    data.setHeight(setHeight(data.getHeight()));
                    log.error("{}", data);
                    cachedDataList.add(data);
                    if (cachedDataList.size() >= BATCH_COUNT) {saveData();
                        cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
                    }
                }

                private String setHeight(String height) {return new BigDecimal(height).add(new BigDecimal("200000")).toString();}

                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {saveData();
                }

                private void saveData() {log.info("{}条数据,开始存储数据库!", cachedDataList.size());
                    log.info("存储数据库胜利!");
                }
            }).sheet().doRead();
        } catch (Exception e) {e.printStackTrace();
        }
        return "ok";
    }
退出移动版