最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java復雜表頭的Excel導入功能實現(xiàn)

 更新時間:2025年09月22日 09:27:21   作者:鍵盤俠Gu  
本篇文章主要介紹如何使用Java相關的技術,來實現(xiàn)復雜表頭的excel導入功能,本文結合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

1、概述

在實現(xiàn)Java中的excel導入功能是,有時會遇到復雜的表頭結構,即表頭由多行或者多列組成,并且包含合并單元格等復雜的布局。本篇文章主要介紹如何使用Java相關的技術,來實現(xiàn)復雜表頭的excel導入功能。

2、實現(xiàn)步驟

  • 提前準備好Excel模板文件和相關的pom文件依賴庫;
  • 解析表頭,并創(chuàng)建與表頭相同的實體類;
  • 編寫監(jiān)聽器,監(jiān)聽Excel實體類相關注解,進行解析;
  • 使用EasyExcel進行文件導入

3、代碼實現(xiàn)

首先創(chuàng)建一個與表頭想呼應的實體類,例如

對應實體類如下,

package com.guyk.web.domain.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
 * @ClassName:ProtectionZoneExcel
 * @Author: guyk
 * @Date: 2025/2/26 14:14
 * @Description:
 */
@Data
public class ProtectionZoneExcel {
    private static final long serialVersionUID = 1L;
    @ExcelProperty(value = {"序號", "序號", "序號", "序號"}, index = 0)
    private String id;
    @ApiModelProperty("河流名稱")
    @TableField("river_name")
    @ExcelProperty(value = {"河流", "河流名稱", "河流名稱", "河流名稱"}, index = 1)
    private String riverName;
    @ApiModelProperty("河流編碼")
    @TableField("river_code")
    @ExcelProperty(value = {"河流", "河流編碼", "河流編碼", "河流編碼"}, index = 2)
    private String riverCode;
    @ApiModelProperty("保護區(qū)名稱")
    @TableField("protected_area_name")
    @ExcelProperty(value = {"防洪保護區(qū)信息", "保護區(qū)名稱", "保護區(qū)名稱", "保護區(qū)名稱"}, index = 3)
    private String protectedAreaName;
    @ApiModelProperty("防護類型")
    @TableField("protection_type")
    @ExcelProperty(value = {"防洪保護區(qū)信息", "防護類型", "防護類型", "防護類型"}, index = 4)
    private String protectionType;
    @ApiModelProperty("面積(平方千米)")
    @TableField("square_measure")
    @ExcelProperty(value = {"防洪保護區(qū)信息", "面積(km2)", "面積(km2)", "面積(km2)"}, index = 5)
    private String squareMeasure;
    @ApiModelProperty("地圖坐標-經(jīng)度")
    @TableField("map_lng")
    @ExcelProperty(value = {"防洪保護區(qū)信息", "地圖坐標", "東經(jīng)", "東經(jīng)"}, index = 6)
    private String mapLng;
    @ApiModelProperty("地圖坐標-緯度")
    @TableField("map_lat")
    @ExcelProperty(value = {"防洪保護區(qū)信息", "地圖坐標", "北緯", "北緯"}, index = 7)
    private String mapLat;
    @ApiModelProperty("現(xiàn)狀防洪標準")
    @TableField("current_flood_control_standards")
    @ExcelProperty(value = {"防洪保護區(qū)信息", "防洪標準", "現(xiàn)狀", "現(xiàn)狀"}, index = 8)
    private String currentFloodControlStandards;
    @ApiModelProperty("設計防洪標準")
    @TableField("design_flood_control_standards")
    @ExcelProperty(value = {"防洪保護區(qū)信息", "防洪標準", "設計", "設計"}, index = 9)
    private String designFloodControlStandards;
    @ApiModelProperty("城鎮(zhèn)-名稱")
    @TableField("town_name")
    @ExcelProperty(value = {"保護對象", "城鎮(zhèn)", "名稱", "名稱"}, index = 10)
    private String townName;
    @ApiModelProperty("城鎮(zhèn)-常住人口(萬人)")
    @TableField("town_permanent_population")
    @ExcelProperty(value = {"保護對象", "城鎮(zhèn)", "常住人口(萬人)", "常住人口(萬人)"}, index = 11)
    private String townPermanentPopulation;
    @ApiModelProperty("城鎮(zhèn)-GDP(億元)")
    @TableField("town_gdp")
    @ExcelProperty(value = {"保護對象", "城鎮(zhèn)", "GDP(億元)", "GDP(億元)"}, index = 12)
    private String townGdp;
    @ApiModelProperty("城鎮(zhèn)-經(jīng)度")
    @TableField("town_lng")
    @ExcelProperty(value = {"保護對象", "城鎮(zhèn)", "坐標", "東經(jīng)"}, index = 13)
    private String townLng;
    @ApiModelProperty("城鎮(zhèn)-緯度")
    @TableField("town_lat")
    @ExcelProperty(value = {"保護對象", "城鎮(zhèn)", "坐標", "北緯"}, index = 14)
    private String townLat;
    @ApiModelProperty("鄉(xiāng)村-名稱")
    @TableField("countryside_name")
    @ExcelProperty(value = {"保護對象", "鄉(xiāng)村", "名稱", "名稱"}, index = 15)
    private String countrysideName;
    @ApiModelProperty("鄉(xiāng)村-常住人口(萬人)")
    @TableField("countryside_permanent_population")
    @ExcelProperty(value = {"保護對象", "鄉(xiāng)村", "常住人口(萬人)", "常住人口(萬人)"}, index = 16)
    private String countrysidePermanentPopulation;
    @ApiModelProperty("鄉(xiāng)村-經(jīng)度")
    @TableField("countryside_lng")
    @ExcelProperty(value = {"保護對象", "鄉(xiāng)村", "坐標", "東經(jīng)"}, index = 17)
    private String countrysideLng;
    @ApiModelProperty("鄉(xiāng)村-緯度")
    @TableField("countryside_lat")
    @ExcelProperty(value = {"保護對象", "鄉(xiāng)村", "坐標", "北緯"}, index = 18)
    private String countrysideLat;
    @ApiModelProperty("耕地-面積(萬畝)")
    @TableField("cropland_square_measure")
    @ExcelProperty(value = {"保護對象", "耕地", "面積(萬畝)", "面積(萬畝)"}, index = 19)
    private String croplandSquareMeasure;
    @ApiModelProperty("耕地-基本農(nóng)田(萬畝)")
    @TableField("cropland_basic_farmland")
    @ExcelProperty(value = {"保護對象", "耕地", "其中:基本農(nóng)田(萬畝)", "其中:基本農(nóng)田(萬畝)"}, index = 20)
    private String croplandBasicFarmland;
    @ApiModelProperty("耕地-經(jīng)度")
    @TableField("cropland_lng")
    @ExcelProperty(value = {"保護對象", "耕地", "坐標", "東經(jīng)"}, index = 21)
    private String croplandLng;
    @ApiModelProperty("耕地-緯度")
    @TableField("cropland_lat")
    @ExcelProperty(value = {"保護對象", "耕地", "坐標", "北緯"}, index = 22)
    private String croplandLat;
    @ApiModelProperty("重要基礎設施-名稱")
    @TableField("critical_infrastructure_name")
    @ExcelProperty(value = {"保護對象", "重要基礎設施", "名稱", "名稱"}, index = 23)
    private String criticalInfrastructureName;
    @ApiModelProperty("重要基礎設施-防護等級")
    @TableField("critical_infrastructure_protection_grade")
    @ExcelProperty(value = {"保護對象", "重要基礎設施", "防護等級", "防護等級"}, index = 24)
    private String criticalInfrastructureProtectionGrade;
    @ApiModelProperty("重要基礎設施-經(jīng)度")
    @TableField("critical_infrastructure_lng")
    @ExcelProperty(value = {"保護對象", "重要基礎設施", "坐標", "東經(jīng)"}, index = 25)
    private String criticalInfrastructureLng;
    @ApiModelProperty("重要基礎設施-緯度")
    @TableField("critical_infrastructure_lat")
    @ExcelProperty(value = {"保護對象", "重要基礎設施", "坐標", "北緯"}, index = 26)
    private String criticalInfrastructureLat;
    @ExcelProperty(value = {"備注", "備注", "備注", "備注"}, index = 27)
    private String remark;
}

其中,實體類中的注解及其含義如下

  • @ApiModelProperty注解:swagger注解。為API木星的屬性提供額外的描述信息,為了生成更清晰的接口文檔。常用屬性如下
    常用屬性
    屬性說明示例
    value字段描述@ApiModelProperty(value = "用戶ID")
    example示例值@ApiModelProperty(example = "123")
    required是否必填@ApiModelProperty(required = true)
    hidden隱藏字段@ApiModelProperty(hidden = true)
    dataType數(shù)據(jù)類型(可選)@ApiModelProperty(dataType = "java.lang.Integer")
  • @TableField注解:mybatis-plus注解。用于標注實體類(Entity)的字段與數(shù)據(jù)庫表的列之間的映射關系。它提供了更靈活的數(shù)據(jù)庫字段與Java實體屬性的對應方式,并支持一些特殊操作(如自動填充,邏輯刪除等)。

    常用屬性
    屬性類型說明示例
    valueString數(shù)據(jù)庫字段名(默認按駝峰轉下劃線)@TableField("user_name")
    existboolean是否對應數(shù)據(jù)庫字段(默認true,若false表示非數(shù)據(jù)庫字段)@TableField(exist = false)
    fillFieldFill自動填充策略(如插入/更新時填充)@TableField(fill = FieldFill.INSERT)
    selectboolean查詢時是否包含該字段(默認true)@TableField(select = false)
    conditionStringWHERE條件預處理(較少用)@TableField(condition = "%s=#{%s}")
  • @ExcelProperty注解:EasyExcel框架中的注解,用于定義Java對象屬性與Excel表格列之間的映射關系,主要用于Excel的導入和導出。

    常用屬性
    屬性類型說明示例
    valueString[]Excel列名(支持多級表頭)@ExcelProperty("姓名")
    indexint列索引(從0開始)@ExcelProperty(index = 0)
    converterClass自定義數(shù)據(jù)轉換器@ExcelProperty(converter = CustomConverter.class)
    formatString日期/數(shù)字格式@ExcelProperty(format = "yyyy-MM-dd")
    ignoreboolean是否忽略該字段@ExcelProperty(ignore = true)

監(jiān)聽器

package com.guyk.web.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.guyk.web.domain.dto.ProtectionZoneExcel;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
 * @ClassName:ProtectionZoneImportExcelListener
 * @Author: guyk
 * @Date: 2025/2/26 14:37
 * @Description:
 */
public class ProtectionZoneImportExcelListener extends AnalysisEventListener<ProtectionZoneExcel> {
    private List<ProtectionZoneExcel> list = new ArrayList<>();
    private List<ProtectionZoneExcel> filteredList = new ArrayList<>();
    @Override
    public void invoke(ProtectionZoneExcel protectionZoneExcel, AnalysisContext analysisContext) {
        list.add(protectionZoneExcel);
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        filteredList = list.stream().filter(protectionZoneExcel -> Objects.nonNull(protectionZoneExcel.getId())).filter(protectionZoneExcel -> protectionZoneExcel.getId().matches("\\d+")).collect(Collectors.toList());
    }
    public List<ProtectionZoneExcel> getFilteredList() {
        return filteredList;
    }
    public List<ProtectionZoneExcel> getList() {
        return list;
    }
}

上傳接口

@ApiOperation("導入保護區(qū)Excel")
@PostMapping("/protectionZoneImport")
public R<Boolean> protectionZoneImport(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
    InputStream inputStream = null;
    try {
        inputStream = file.getInputStream();
        ProtectionZoneImportExcelListener listener = new ProtectionZoneImportExcelListener();
        EasyExcel.read(inputStream, ProtectionZoneExcel.class, listener).sheet().doRead();
        // 導入之后,入庫
        log.info("解析成功!準備處理!");
        return R.ok(this.protectionZoneService.protectionZoneImport(listener));
    } catch (IOException e) {
        log.info("入庫失??!", e);
        return R.fail(Boolean.FALSE);
    }
}

到此這篇關于Java復雜表頭的Excel導入的文章就介紹到這了,更多相關java excel導入復雜表頭內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • mybatis Invalid bound statement(not found)排坑記錄

    mybatis Invalid bound statement(not foun

    這篇文章主要介紹了mybatis Invalid bound statement(not found)排坑記錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • IDEA中程序包Org.Springframework.Boot不存在問題及解決

    IDEA中程序包Org.Springframework.Boot不存在問題及解決

    這篇文章主要介紹了IDEA中程序包Org.Springframework.Boot不存在問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Spring boot2X Consul如何通過RestTemplate實現(xiàn)服務調(diào)用

    Spring boot2X Consul如何通過RestTemplate實現(xiàn)服務調(diào)用

    這篇文章主要介紹了spring boot2X Consul如何通過RestTemplate實現(xiàn)服務調(diào)用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • ClassLoader雙親委派模式作用詳解

    ClassLoader雙親委派模式作用詳解

    這篇文章主要為大家介紹了ClassLoader雙親委派模式作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • 任何Bean通過實現(xiàn)ProxyableBeanAccessor接口即可獲得動態(tài)靈活的獲取代理對象或原生對象的能力(最新推薦)

    任何Bean通過實現(xiàn)ProxyableBeanAccessor接口即可獲得動態(tài)靈活的獲取代理對象或原生對象的能力(最新推

    這篇文章主要介紹了任何Bean通過實現(xiàn)ProxyableBeanAccessor接口即可獲得動態(tài)靈活的獲取代理對象或原生對象的能力,通過示例代碼看到,借助ProxyableBeanAccessor接口默認實現(xiàn)的getReal、getProxy、selfAs方法,很靈活的按需獲取代理或非代理對象,需要的朋友可以參考下
    2024-02-02
  • 前端WebSocket連接失敗問題的排查過程及解決

    前端WebSocket連接失敗問題的排查過程及解決

    WebSocket是一種全雙工通信協(xié)議,它允許通過單個長久的TCP連接在客戶端和服務器之間進行實時雙向通信,這篇文章主要介紹了前端WebSocket連接失敗問題的排查過程及解決辦法,需要的朋友可以參考下
    2025-07-07
  • Spring Boot如何整合FreeMarker模板引擎

    Spring Boot如何整合FreeMarker模板引擎

    這篇文章主要介紹了Spring Boot如何整合FreeMarker模板引擎,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • Java源碼分析:Guava之不可變集合ImmutableMap的源碼分析

    Java源碼分析:Guava之不可變集合ImmutableMap的源碼分析

    今天給大家?guī)淼氖顷P于Java源碼的相關知識,文章圍繞著Java ImmutableMap展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下,希望能給你帶來幫助
    2021-06-06
  • Mybatis打印SQL語句的幾種常用方式

    Mybatis打印SQL語句的幾種常用方式

    當我們動態(tài)sql拼接的塊很多的時候,我們要想從*mapper.xml中直接找出完整的sql就會非常的難,這個時候經(jīng)常會需要把組合之后的完整SQL語句打印出來,對調(diào)試非常有幫助的,所以本文小編給大家介紹了幾種Mybatis打印SQL語句的常用方式,需要的朋友可以參考下
    2023-11-11
  • Java常用的Arrays函數(shù)舉例詳解

    Java常用的Arrays函數(shù)舉例詳解

    這篇文章主要給大家介紹了關于Java常用Arrays函數(shù)舉例詳解的相關資料,Java數(shù)組常用方法包括sort、toString、equals、fill、copyOf等,涵蓋數(shù)組排序、比較、填充與拷貝,需要的朋友可以參考下
    2025-05-05

最新評論

格尔木市| 郧西县| 蕉岭县| 永德县| 合作市| 吉水县| 华宁县| 墨竹工卡县| 昌黎县| 宕昌县| 巴林左旗| 肇源县| 顺义区| 青神县| 长葛市| 宁明县| 汉阴县| 英山县| 临颍县| 康平县| 南充市| 庆安县| 珲春市| 浑源县| 肇庆市| 开鲁县| 雅江县| 长治县| 鹤壁市| 腾冲县| 鸡泽县| 信阳市| 保德县| 祁阳县| 镇雄县| 耿马| 比如县| 昌黎县| 农安县| 南江县| 台湾省|