EasyExcel 简单读取Excel(无需监听器)

EasyExcel是一个Java库,用于快速、简单地读写Excel文件。要使用EasyExcel,您首先需要将其添加为项目的依赖: 

引入 jar 包

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

数据准备:

 读取如图excel 数据,存入数据库

首先准备接受数据的对象dto, 也可以作为excel的表头对象

package com.schdri.bimgis.disaster.line.entity.dto;

import com.schdri.bimgis.disaster.domain.util.ExcelField;
import io.swagger.annotations.ApiModel;
import lombok.Data;

@Data
@ApiModel(value = "路网数据Dto")
public class LineImportDto {
    /**
     * 路线名称
     */
    @ExcelField(value="路线名称")
    private String lineName;

    /**
     * 路线等级
     */
    @ExcelField(value="路线等级")
    private String roadLevelStr;

    /**
     * 起始桩号
     */
    @ExcelField(value="起始桩号")
    private String startStake;

    /**
     * 路线坐标
     */
    @ExcelField(value="路线坐标")
    private String lineCoordinate;
}

 注解:

import java.lang.annotation.*;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface ExcelField {
    String value() default "";
}

 直接上代码,读取数据:

public ResponseMessage importExcel(MultipartFile file) {
    try {
        String path = "export/路线demo.xlsx";
        File file2 = new File(path);
        if (!file2.exists()) {
            file2.createNewFile();
        }
        // MultipartFile 转 File
        FileCopyUtils.copy(file.getBytes(), file2);
        InputStream inputStream = new FileInputStream(file2.getPath());
        // LineImportDto -> 接受数据的对象dto, 也可以作为excel的表头对象
        // .sheet()  -> 需要读取的sheet页
        // .doReadSync()   -> 异步读取
        // 异步读取excel数据,放到list集合里
        List<LineImportDto> lineImportDtoList = EasyExcel.read(inputStream).head(LineImportDto.class).sheet().doReadSync();
        // 保存路网数据到数据库
        if (CollectionUtil.isNotEmpty(lineImportDtoList)) {
            log.info("lineImportDtoList:" + JSON.toJSONString(lineImportDtoList));
            // todo 保存数据库。。。。。。。。

        }
        inputStream.close();
        return ResponseMessage.ok();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

打印的数据:

[

    {

        "lineCoordinate": "[[105.3077316,29.2172447],[105.3588867,29.2223357],[105.4632568,29.2079603],[105.4587936,29.173511],[105.4821396,29.1153702],[105.4230881,29.0880867],[105.3568268,29.0829889]]",

        "lineName": "春熙路",

        "roadLevelStr": "四级公路",

        "startStake": "K5+12.65"

    },

    {

        "lineCoordinate": "[[105.3077316,29.2172447],[105.3588867,29.2223357]]",

        "lineName": "南京路",

        "roadLevelStr": "三级公路",

        "startStake": "K5+12.65"

    },

    {

        "lineCoordinate": "[[105.3077316,29.2172447],[105.3588867,29.2223357]]",

        "lineName": "西华路",

        "roadLevelStr": "一级公路",

        "startStake": "K5+13.66"

    },

    {

        "lineCoordinate": "[[105.3077316,29.2172447],[105.3588867,29.2223357]]",

        "lineName": "你修的路",

        "roadLevelStr": "二级公路",

        "startStake": "K5+14.67"

    },

    {

        "lineCoordinate": "[[105.3077316,29.2172447],[105.3588867,29.2223357]]",

        "lineName": "我修的路",

        "roadLevelStr": "其他乡村道路",

        "startStake": "K5+15.68"

    }

]

简单完成,收工摸鱼,,,,

猜你喜欢

转载自blog.csdn.net/m0_56324585/article/details/131726670
今日推荐