Java?EasyExcel創(chuàng)建復(fù)雜表格的完整指南
1.EasyExcel WriteTable 核心概念解析
WriteTable 是 EasyExcel 庫中的一個核心組件,用于在同一個 Excel 表單(Sheet)內(nèi)創(chuàng)建多個獨(dú)立的表格區(qū)域。與 WriteSheet(代表整個表單)不同,WriteTable 允許您在同一個 Sheet 中構(gòu)建多個結(jié)構(gòu)化表格,每個表格有自己的表頭和數(shù)據(jù)區(qū)域。這種機(jī)制特別適用于需要生成復(fù)雜報(bào)表的場景,如企業(yè)數(shù)據(jù)匯總、財(cái)務(wù)分析等。
技術(shù)要點(diǎn):
- 相對位置定位:WriteTable 通過設(shè)置
relativeHeadRowIndex等參數(shù)來定義表格的起始位置,確保多個表格不會重疊。 - 復(fù)雜表頭支持:支持多級表頭(如三級嵌套表頭),通過
@ExcelProperty注解實(shí)現(xiàn)。 - 樣式定制:允許自定義表格樣式,包括表頭背景色、字體、邊框等。
- 大數(shù)據(jù)量處理:基于 EasyExcel 的流式寫入機(jī)制,能高效處理大量數(shù)據(jù),避免內(nèi)存溢出。
2.環(huán)境準(zhǔn)備與基礎(chǔ)配置
2.1 Maven 依賴配置
在您的 Java 項(xiàng)目中,需添加 EasyExcel 和 Lombok 依賴。Lombok 用于簡化實(shí)體類定義(可選)。在 pom.xml 文件中添加以下內(nèi)容:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<optional>true</optional>
</dependency>
</dependencies>
2.2 基礎(chǔ)實(shí)體類定義
定義數(shù)據(jù)模型實(shí)體類,用于表示表格數(shù)據(jù)。使用 @ExcelProperty 注解指定表頭名稱。
@Data
public class Person {
@ExcelProperty("ID")
private Integer id;
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年齡")
private Integer age;
@ExcelProperty("入職日期")
private Date joinDate;
}
@Data
public class Department {
@ExcelProperty("部門ID")
private Integer deptId;
@ExcelProperty("部門名稱")
private String deptName;
@ExcelProperty("預(yù)算(萬元)")
private Double budget;
}
3.核心實(shí)現(xiàn):多表格創(chuàng)建
基礎(chǔ)多表格實(shí)現(xiàn)
在服務(wù)類中,使用 WriteTable 創(chuàng)建多個表格。關(guān)鍵步驟包括:
- 創(chuàng)建 ExcelWriter:初始化寫入器。
- 定義 WriteSheet:代表整個表單,可禁用默認(rèn)表頭。
- 定義 WriteTable:每個表格指定表頭類、位置和樣式。
- 寫入數(shù)據(jù):為每個表格提供數(shù)據(jù)源。
| ID | 姓名 | 年齡 | 入職日期 |
|---|---|---|---|
| 1 | power | 22 | 2024-10-12 |
| 2 | 小明 | 32 | 2021-02-21 |
| 部門id | 部門名稱 | 預(yù)算(萬元) | |
| b101 | 財(cái)務(wù)部 | 2000 |
示例代碼:
@Service
public class ComplexExcelService {
public void writeComplexTable() {
/*
//1.寫入io流
ByteArrayOutputStream outputStream =new ByteArrayOutputStream(1024);
ExcelWriter excelWriter = EasyExcel.write(outputStream).build();
*/
//2。寫入文件
String fileName = "企業(yè)數(shù)據(jù)報(bào)表_" + LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE) + ".xlsx";
try (ExcelWriter excelWriter = EasyExcel.write(fileName).build()) {
// 創(chuàng)建 WriteSheet,禁用默認(rèn)表頭
//.registerWriteHandler(XXXHandler) (XXXHandler需實(shí)現(xiàn) CellWriteHandler)
WriteSheet writeSheet = EasyExcel.writerSheet("企業(yè)綜合數(shù)據(jù)")
.needHead(Boolean.FALSE)
.build();
// 創(chuàng)建第一個表格:人員信息
WriteTable personnelTable = EasyExcel.writerTable(0)
.head(Person.class) // 使用類注解生成表頭
.needHead(Boolean.TRUE)
.build(); // 可添加 .tableStyle() 自定義樣式
// 創(chuàng)建第二個表格:部門信息,定位在第一個表格下方
WriteTable departmentTable = EasyExcel.writerTable(1)
.head(Department.class)
.needHead(Boolean.TRUE)
.relativeHeadRowIndex(6) // 預(yù)留空間,避免重疊
.build();
// 寫入數(shù)據(jù)
excelWriter.write(generatePersonData(), writeSheet, personnelTable);
excelWriter.write(generateDepartmentData(), writeSheet, departmentTable);
logger.info("復(fù)雜表格生成完成:{}", fileName);
}
}
private List<Person> generatePersonData() {
// 生成模擬數(shù)據(jù),略
}
private List<Department> generateDepartmentData() {
// 生成模擬數(shù)據(jù),略
}
}
關(guān)鍵參數(shù)解釋:
relativeHeadRowIndex:指定表格表頭的起始行索引(從 0 開始),用于定位表格位置。tableStyle:可選參數(shù),用于自定義表格樣式(需實(shí)現(xiàn)WriteHandler)。
4.高級特性:復(fù)雜表頭與樣式定制
4.1 多級表頭實(shí)現(xiàn)
EasyExcel 支持嵌套表頭,通過 @ExcelProperty 的數(shù)組參數(shù)定義多級結(jié)構(gòu)。
@Data
@HeadRowHeight(25)
@ContentRowHeight(20)
public class ComplexHeaderData {
@ExcelProperty(value = {"企業(yè)信息", "基本信息", "員工編號"})
private String employeeId;
@ExcelProperty(value = {"企業(yè)信息", "基本信息", "姓名"})
private String name;
@ExcelProperty(value = {"財(cái)務(wù)數(shù)據(jù)", "薪資信息", "基本工資"})
private Double baseSalary;
@ExcelProperty(value = {"財(cái)務(wù)數(shù)據(jù)", "薪資信息", "績效獎金"})
private Double performanceBonus;
@ExcelProperty(value = {"財(cái)務(wù)數(shù)據(jù)", "薪資信息", "年終獎"})
private Double annualBonus;
}
在 WriteTable 中,使用此類作為表頭,即可自動生成三級表頭。
4.2 自定義樣式策略
通過實(shí)現(xiàn) WriteHandler 接口,可以定制表頭和數(shù)據(jù)行的樣式。
@Component
public class CustomExcelStyleStrategy implements WriteHandler {
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder,
WriteTableHolder writeTableHolder, Cell cell, Head head,
Integer relativeRowIndex, Boolean isHead) {
Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
CellStyle style = workbook.createCellStyle();
if (isHead) {
// 表頭樣式:藍(lán)色背景,白色文字
style.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
style.setFont(font);
// 邊框設(shè)置
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
} else {
// 數(shù)據(jù)行樣式:灰色邊框
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
}
cell.setCellStyle(style);
}
}
在 WriteTable 構(gòu)建時,添加此策略:
WriteTable personnelTable = EasyExcel.writerTable(0)
.head(Person.class)
.needHead(Boolean.TRUE)
.registerWriteHandler(new CustomExcelStyleStrategy()) // 注冊自定義樣式
.build();
5.總結(jié)
WriteTable 是 EasyExcel 中實(shí)現(xiàn)多表格報(bào)表的核心工具,通過相對位置定位和樣式定制,能高效生成復(fù)雜 Excel 文件。關(guān)鍵步驟包括:
- 準(zhǔn)備環(huán)境:添加依賴,定義實(shí)體類。
- 構(gòu)建表格:使用
WriteTable定義多個獨(dú)立區(qū)域。 - 高級定制:利用多級表頭和自定義樣式增強(qiáng)可讀性。
到此這篇關(guān)于Java EasyExcel創(chuàng)建復(fù)雜表格的完整指南的文章就介紹到這了,更多相關(guān)Java EasyExcel創(chuàng)建復(fù)雜表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于springmvc-servlet中的配置小知識詳解
這篇文章主要介紹了關(guān)于springmvc-servlet中的配置小知識詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Mybatis-plus配置之日期時間自動填充實(shí)踐
本文介紹如何使用MyBatis-Plus的MetaObjectHandler接口實(shí)現(xiàn)新增和更新時間的自動填充,通過繼承抽象類、添加注解及處理版本兼容性,簡化開發(fā)流程并減少手動操作2025-08-08
關(guān)于Kafka消息隊(duì)列原理的總結(jié)
這篇文章主要介紹了關(guān)于Kafka消息隊(duì)列原理的總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
SpringBoot+BootStrap多文件上傳到本地實(shí)例
這篇文章主要介紹了SpringBoot+BootStrap多文件上傳到本地實(shí)例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringCloudGateway 網(wǎng)關(guān)登錄校驗(yàn)實(shí)現(xiàn)思路
文章介紹了在微服務(wù)架構(gòu)中使用Spring Cloud Gateway進(jìn)行登錄校驗(yàn)的方法,通過在網(wǎng)關(guān)層面進(jìn)行登錄校驗(yàn),并將用戶信息通過請求頭傳遞給下游微服務(wù),解決了每個微服務(wù)都需要獨(dú)立進(jìn)行登錄校驗(yàn)的問題,此外,還討論了如何在微服務(wù)之間傳遞用戶信息2024-11-11
Spring中byName和byType的區(qū)別及說明
這篇文章主要介紹了Spring中byName和byType的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Spring MVC-@RequestMapping注解詳解
@RequestMapping注解的作用,就是將請求和處理請求的控制器方法關(guān)聯(lián)起來,建立映射關(guān)系。這篇文章主要給大家介紹了關(guān)于SpringMVC中@RequestMapping注解用法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04

