Java復(fù)雜表頭的Excel導(dǎo)入功能實現(xiàn)
1、概述
在實現(xiàn)Java中的excel導(dǎo)入功能是,有時會遇到復(fù)雜的表頭結(jié)構(gòu),即表頭由多行或者多列組成,并且包含合并單元格等復(fù)雜的布局。本篇文章主要介紹如何使用Java相關(guān)的技術(shù),來實現(xiàn)復(fù)雜表頭的excel導(dǎo)入功能。
2、實現(xiàn)步驟
- 提前準備好Excel模板文件和相關(guān)的pom文件依賴庫;
- 解析表頭,并創(chuàng)建與表頭相同的實體類;
- 編寫監(jiān)聽器,監(jiān)聽Excel實體類相關(guān)注解,進行解析;
- 使用EasyExcel進行文件導(dǎo)入
3、代碼實現(xiàn)
首先創(chuàng)建一個與表頭想呼應(yīng)的實體類,例如

對應(yī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("設(shè)計防洪標準")
@TableField("design_flood_control_standards")
@ExcelProperty(value = {"防洪保護區(qū)信息", "防洪標準", "設(shè)計", "設(shè)計"}, 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("重要基礎(chǔ)設(shè)施-名稱")
@TableField("critical_infrastructure_name")
@ExcelProperty(value = {"保護對象", "重要基礎(chǔ)設(shè)施", "名稱", "名稱"}, index = 23)
private String criticalInfrastructureName;
@ApiModelProperty("重要基礎(chǔ)設(shè)施-防護等級")
@TableField("critical_infrastructure_protection_grade")
@ExcelProperty(value = {"保護對象", "重要基礎(chǔ)設(shè)施", "防護等級", "防護等級"}, index = 24)
private String criticalInfrastructureProtectionGrade;
@ApiModelProperty("重要基礎(chǔ)設(shè)施-經(jīng)度")
@TableField("critical_infrastructure_lng")
@ExcelProperty(value = {"保護對象", "重要基礎(chǔ)設(shè)施", "坐標", "東經(jīng)"}, index = 25)
private String criticalInfrastructureLng;
@ApiModelProperty("重要基礎(chǔ)設(shè)施-緯度")
@TableField("critical_infrastructure_lat")
@ExcelProperty(value = {"保護對象", "重要基礎(chǔ)設(shè)施", "坐標", "北緯"}, 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ù)庫表的列之間的映射關(guān)系。它提供了更靈活的數(shù)據(jù)庫字段與Java實體屬性的對應(yīng)方式,并支持一些特殊操作(如自動填充,邏輯刪除等)。
常用屬性 屬性 類型 說明 示例 value String 數(shù)據(jù)庫字段名(默認按駝峰轉(zhuǎn)下劃線) @TableField("user_name") exist boolean 是否對應(yīng)數(shù)據(jù)庫字段(默認true,若false表示非數(shù)據(jù)庫字段) @TableField(exist = false) fill FieldFill 自動填充策略(如插入/更新時填充) @TableField(fill = FieldFill.INSERT) select boolean 查詢時是否包含該字段(默認true) @TableField(select = false) condition String WHERE條件預(yù)處理(較少用) @TableField(condition = "%s=#{%s}") @ExcelProperty注解:EasyExcel框架中的注解,用于定義Java對象屬性與Excel表格列之間的映射關(guān)系,主要用于Excel的導(dǎo)入和導(dǎo)出。
常用屬性 屬性 類型 說明 示例 value String[] Excel列名(支持多級表頭) @ExcelProperty("姓名") index int 列索引(從0開始) @ExcelProperty(index = 0) converter Class 自定義數(shù)據(jù)轉(zhuǎn)換器 @ExcelProperty(converter = CustomConverter.class) format String 日期/數(shù)字格式 @ExcelProperty(format = "yyyy-MM-dd") ignore boolean 是否忽略該字段 @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("導(dǎo)入保護區(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();
// 導(dǎo)入之后,入庫
log.info("解析成功!準備處理!");
return R.ok(this.protectionZoneService.protectionZoneImport(listener));
} catch (IOException e) {
log.info("入庫失?。?, e);
return R.fail(Boolean.FALSE);
}
}到此這篇關(guān)于Java復(fù)雜表頭的Excel導(dǎo)入的文章就介紹到這了,更多相關(guān)java excel導(dǎo)入復(fù)雜表頭內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java?easyExcel的復(fù)雜表頭多級表頭導(dǎo)入
- Java如何利用Easyexcel動態(tài)導(dǎo)出表頭列
- Java使用EasyExcel生成動態(tài)表頭和多Sheet數(shù)據(jù)的Excel實例
- Java結(jié)合EasyExcel構(gòu)建復(fù)雜多級表頭
- Java導(dǎo)出Excel動態(tài)表頭的示例詳解
- java poi實現(xiàn)Excel多級表頭導(dǎo)出方式(多級表頭,復(fù)雜表頭)
- Java利用EasyExcel解析動態(tài)表頭及導(dǎo)出實現(xiàn)過程
- Java easyExcel實現(xiàn)導(dǎo)入多sheet的Excel
相關(guān)文章
SpringBoot利用dag加速Spring beans初始化的方法示例
本文介紹了利用DAG加速SpringBoot中Spring beans初始化,先解釋了DAG 概念及特性,包括節(jié)點入度出度、拓撲排序等,接著闡述加速Spring Bean初始化的實現(xiàn)思路,如識別依賴關(guān)系構(gòu)建DAG、拓撲排序、并行初始化Bean及與Spring集成,還展示了相關(guān)代碼工程、測試結(jié)果及引用2024-12-12
詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑
這篇文章主要介紹了詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
MyBatis 接收數(shù)據(jù)庫中沒有的字段的解決
這篇文章主要介紹了MyBatis 接收數(shù)據(jù)庫中沒有的字段的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Spring6.x對調(diào)度和異步執(zhí)行的注解支持示例詳解
這篇文章主要為大家介紹了Spring6.x對調(diào)度和異步執(zhí)行的注解支持示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
SpringHateoas超媒體API之資源表示與鏈接關(guān)系詳解
本文將深入探討Spring HATEOAS的核心概念、資源表示方式以及如何構(gòu)建豐富的超媒體API,幫助開發(fā)者創(chuàng)建更具自描述性和可發(fā)現(xiàn)性的Web服務(wù),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04

