SpringBoot整合EasyPoi實(shí)現(xiàn)復(fù)雜多級(jí)表頭Excel導(dǎo)出的完整方案
以下是基于 Spring Boot 整合 EasyPoi 實(shí)現(xiàn)復(fù)雜多級(jí)表頭 Excel 導(dǎo)出的完整方案,包含架構(gòu)設(shè)計(jì)、核心代碼和關(guān)鍵配置說(shuō)明:
一、技術(shù)選型方案
graph LR A[Spring Boot] --> B[Controller層] B --> C[Service層] C --> D[Entity實(shí)體類] D --> E[EasyPoi導(dǎo)出引擎] E --> F[Excel文件]
二、環(huán)境配置
- ??Maven 依賴??
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.4.0</version>
</dependency>
- ??Spring Boot 配置??
# Excel 導(dǎo)出配置 easypoi.export.head-rows=1 easypoi.export.sheet-name=Sheet1
三、核心實(shí)現(xiàn)
1. 多級(jí)表頭實(shí)體類定義
@Data
public class FinancialReport {
// 一級(jí)表頭:財(cái)務(wù)概覽
@Excel(name = "財(cái)務(wù)概覽/總收入", orderNum = "0", width = 20)
private BigDecimal totalIncome;
// 二級(jí)表頭嵌套
@Excel(name = "部門(mén)明細(xì)", orderNum = "1")
private Department department;
// 集合類型表頭
@ExcelCollection(name = "項(xiàng)目明細(xì)")
private List<Project> projects;
}
@Data
public class Department {
@Excel(name = "部門(mén)名稱", orderNum = "0", width = 15)
private String deptName;
@Excel(name = "人員規(guī)模", orderNum = "1", width = 10)
private Integer staffCount;
}
@Data
public class Project {
@Excel(name = "項(xiàng)目名稱", orderNum = "0", width = 25)
private String projectName;
// 三級(jí)表頭
@Excel(name = "成本明細(xì)/人力成本", orderNum = "1", width = 15)
private BigDecimal laborCost;
@Excel(name = "成本明細(xì)/設(shè)備成本", orderNum = "2", width = 15)
private BigDecimal equipmentCost;
}
2. 導(dǎo)出工具類實(shí)現(xiàn)
public class ExcelExportUtil {
public static Workbook createWorkbook(List<FinancialReport> data) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("財(cái)務(wù)報(bào)告");
// 創(chuàng)建表頭行
Row headerRow = sheet.createRow(0);
// 動(dòng)態(tài)構(gòu)建多級(jí)表頭
buildHeaderRow(headerRow, 0, data.getClass().getDeclaredFields());
// 填充數(shù)據(jù)
int rowNum = 1;
for (FinancialReport report : data) {
Row row = sheet.createRow(rowNum++);
fillDataRow(row, report);
}
// 自動(dòng)調(diào)整列寬
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
sheet.autoSizeColumn(i);
}
return workbook;
}
private static void buildHeaderRow(Row row, int level, Field[] fields) {
for (Field field : fields) {
Excel excel = field.getAnnotation(Excel.class);
if (excel != null) {
Cell cell = row.createCell(excel.orderNum());
cell.setCellValue(excel.name());
// 處理合并單元格
if (excel.mergeVertical() || excel.mergeHorizontal()) {
sheet.addMergedRegion(new CellRangeAddress(
0, 0,
excel.orderNum(),
excel.orderNum() + excel.name().split("/").length - 1
));
}
// 遞歸處理嵌套表頭
if (!Collection.class.isAssignableFrom(field.getType())) {
buildHeaderRow(row, level + 1, field.getType().getDeclaredFields());
}
}
}
}
private static void fillDataRow(Row row, Object obj) {
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
Cell cell = row.createCell(field.getAnnotation(Excel.class).orderNum());
cell.setCellValue(field.get(obj).toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
3. Controller 層實(shí)現(xiàn)
@RestController
@RequestMapping("/export")
public class ExcelController {
@Autowired
private ReportService reportService;
@GetMapping("/financial-report")
public void exportFinancialReport(HttpServletResponse response) {
try {
// 生成數(shù)據(jù)
List<FinancialReport> data = reportService.generateReportData();
// 創(chuàng)建工作簿
Workbook workbook = ExcelExportUtil.createWorkbook(data);
// 設(shè)置響應(yīng)頭
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode("財(cái)務(wù)報(bào)告.xlsx", "UTF-8"));
// 寫(xiě)入輸出流
workbook.write(response.getOutputStream());
workbook.close();
} catch (Exception e) {
throw new RuntimeException("導(dǎo)出失敗:" + e.getMessage());
}
}
}
四、關(guān)鍵配置說(shuō)明
1. 表頭合并策略
| 注解屬性 | 作用說(shuō)明 | 示例值 |
|---|---|---|
| mergeVertical | 垂直合并單元格 | true |
| mergeHorizontal | 水平合并單元格 | true |
| orderNum | 列排序序號(hào) | "0" |
| width | 列寬(單位:像素) | 20 |
2. 性能優(yōu)化配置
# 大數(shù)據(jù)量導(dǎo)出配置 easypoi.export.big-data=true easypoi.export.row-access-window-size=100
五、高級(jí)功能實(shí)現(xiàn)
1. 動(dòng)態(tài)表頭構(gòu)建
public class DynamicHeaderBuilder {
public static List<ExcelExportEntity> buildDynamicHeader() {
List<ExcelExportEntity> entities = new ArrayList<>();
// 一級(jí)表頭
ExcelExportEntity parent = new ExcelExportEntity("財(cái)務(wù)分析", "finance");
parent.setNeedMerge(true);
// 二級(jí)表頭
ExcelExportEntity child1 = new ExcelExportEntity("收入分析", "income");
ExcelExportEntity child2 = new ExcelExportEntity("支出分析", "expense");
// 三級(jí)表頭
ExcelExportEntity grandChild1 = new ExcelExportEntity("銷售收入", "sales");
ExcelExportEntity grandChild2 = new ExcelExportEntity("運(yùn)營(yíng)成本", "operating");
// 構(gòu)建層級(jí)關(guān)系
child1.setList(Arrays.asList(grandChild1));
child2.setList(Arrays.asList(grandChild2));
parent.setList(Arrays.asList(child1, child2));
entities.add(parent);
return entities;
}
}
2. 自定義樣式配置
public class CustomStyleUtil {
public static CellStyle createHeaderStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
font.setColor(IndexedColors.WHITE.getIndex());
style.setFont(font);
style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setAlignment(HorizontalAlignment.CENTER);
return style;
}
}
六、常見(jiàn)問(wèn)題解決方案
| 問(wèn)題現(xiàn)象 | 解決方案 |
|---|---|
| 表頭層級(jí)錯(cuò)亂 | 檢查@Excel.name路徑分隔符是否正確,確保/數(shù)量與層級(jí)一致 |
| 合并單元格失效 | 添加@Excel(mergeVertical = true)注解或自定義合并策略 |
| 數(shù)據(jù)類型轉(zhuǎn)換異常 | 使用@Excel(importFormat = "yyyy-MM-dd")指定格式化規(guī)則 |
| 導(dǎo)出文件名亂碼 | 使用URLEncoder.encode()處理文件名編碼 |
| 大數(shù)據(jù)量導(dǎo)出內(nèi)存溢出 | 啟用SXSSF模式:ExcelType.SXSSF并設(shè)置窗口大小 |
七、擴(kuò)展應(yīng)用場(chǎng)景
- ??模板導(dǎo)出??
TemplateExportParams params = new TemplateExportParams("template.xlsx");
Map<String, Object> data = new HashMap<>();
data.put("title", "財(cái)務(wù)報(bào)告");
data.put("detail", reportData);
Workbook workbook = ExcelExportUtil.exportExcel(params, data);
- ??動(dòng)態(tài)列生成??
List<ExcelExportEntity> dynamicColumns = new ArrayList<>();
dynamicColumns.add(new ExcelExportEntity("動(dòng)態(tài)列1", "field1"));
dynamicColumns.add(new ExcelExportEntity("動(dòng)態(tài)列2", "field2"));
ExcelExportUtil.exportBigExcel(params, dynamicColumns, data);
通過(guò)上述方案,可以實(shí)現(xiàn)包含4級(jí)以上嵌套表頭的復(fù)雜Excel導(dǎo)出需求。建議結(jié)合注解和動(dòng)態(tài)表頭構(gòu)建兩種方式,靈活應(yīng)對(duì)不同場(chǎng)景的表頭結(jié)構(gòu)要求。實(shí)際開(kāi)發(fā)中需注意數(shù)據(jù)驗(yàn)證和異常處理機(jī)制。
以上就是SpringBoot整合EasyPoi實(shí)現(xiàn)復(fù)雜多級(jí)表頭Excel導(dǎo)出的完整方案的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot EasyPoi多級(jí)表頭Excel導(dǎo)出的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring boot項(xiàng)目集成Camel FTP的方法示例
這篇文章主要介紹了Spring boot項(xiàng)目集成Camel FTP的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
java基礎(chǔ)之初始化ArrayList時(shí)直接賦值的4種方式總結(jié)
ArrayList是Java中的一個(gè)類,它是Java集合框架中的一部分,用于實(shí)現(xiàn)動(dòng)態(tài)數(shù)組,下面這篇文章主要給大家介紹了關(guān)于java基礎(chǔ)之初始化ArrayList時(shí)直接賦值的4種方式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
用java生成html文件實(shí)現(xiàn)原理及代碼
用printStream來(lái)向html文件里輸出數(shù)據(jù),先創(chuàng)建一個(gè)StringBuilder對(duì)象,通過(guò)append方法來(lái)為其添加html語(yǔ)句,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08
java如何從地址串中解析提取省市區(qū)(完美匹配中國(guó)所有地址)
這篇文章主要給大家介紹了關(guān)于java如何從地址串中解析提取省市區(qū)的相關(guān)資料,通過(guò)這個(gè)方法可以完美匹配中國(guó)所有地址,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
使用IDEA開(kāi)發(fā)配置Java Web的初始化過(guò)程
該教程使用idea開(kāi)發(fā)工具初始化javaweb項(xiàng)目,該運(yùn)行在tomcat服務(wù)器上通過(guò)配置項(xiàng)目環(huán)境變量保證tomcat正常啟動(dòng),具體操作配置教程參考下本文2021-06-06
Spring實(shí)戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作,結(jié)合實(shí)例形式分析了Spring協(xié)調(diào)作用域不同步的Bean相關(guān)配置及使用技巧,需要的朋友可以參考下2019-11-11
使用Mybatis-Plus實(shí)現(xiàn)對(duì)象屬性自動(dòng)填充功能
這篇文章主要介紹了如何使用Mybatis-Plus實(shí)現(xiàn)對(duì)象屬性自動(dòng)填充功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,感興趣的朋友們下面隨著小編來(lái)一起來(lái)學(xué)習(xí)吧2024-01-01
POI導(dǎo)出之Excel實(shí)現(xiàn)單元格的背景色填充問(wèn)題
這篇文章主要介紹了POI導(dǎo)出之Excel實(shí)現(xiàn)單元格的背景色填充問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Java?file類中renameTo方法操作實(shí)例
renameTo()方法是File類的一部分,renameTo()函數(shù)用于將文件的抽象路徑名重命名為給定的路徑名??,下面這篇文章主要給大家介紹了關(guān)于Java?file類中renameTo方法操作的相關(guān)資料,需要的朋友可以參考下2022-11-11

