Commit 7d1419dbf0ecc9d95d66dd095402ee991a8f63f0

Authored by zhangmeiyang
1 parent fe281fad

refactor(excel): 优化Excel工具类处理逻辑

- 将fullySyncCustomer接口的@PostMapping注解改为@RequestMapping
- ExcelUtils增加@Slf4j注解用于日志记录
- 使用DataFormatter处理Excel单元格值,提升数据读取准确性
- 改进表头读取逻辑,支持动态列映射
- 增加对空单元格的处理,返回null而非空字符串
- 为TaxTenantController的call方法添加默认"value"字段
tax-adopt/src/main/java/com/diligrp/tax/adopt/api/AdoptMessageController.java
@@ -41,7 +41,7 @@ public class AdoptMessageController { @@ -41,7 +41,7 @@ public class AdoptMessageController {
41 * @param documentType 文档类型 41 * @param documentType 文档类型
42 * @return {@link Message }<{@link ? }> 42 * @return {@link Message }<{@link ? }>
43 */ 43 */
44 - @PostMapping("/fullySyncCustomer/{group}/{entity}/{pipelineCode}/{documentType}") 44 + @RequestMapping("/fullySyncCustomer/{group}/{entity}/{pipelineCode}/{documentType}")
45 public Message<?> fullySyncCustomer(@RequestPart("file") @NotNull MultipartFile file, 45 public Message<?> fullySyncCustomer(@RequestPart("file") @NotNull MultipartFile file,
46 @PathVariable("group") String group, 46 @PathVariable("group") String group,
47 @PathVariable("entity") String entity, 47 @PathVariable("entity") String entity,
tax-adopt/src/main/java/com/diligrp/tax/adopt/util/ExcelUtils.java
@@ -2,6 +2,9 @@ package com.diligrp.tax.adopt.util; @@ -2,6 +2,9 @@ package com.diligrp.tax.adopt.util;
2 2
3 import com.diligrp.tax.central.exception.TaxAgentServiceException; 3 import com.diligrp.tax.central.exception.TaxAgentServiceException;
4 import com.diligrp.tax.central.type.TaxSystemType; 4 import com.diligrp.tax.central.type.TaxSystemType;
  5 +import lombok.extern.slf4j.Slf4j;
  6 +import org.apache.poi.ss.usermodel.Cell;
  7 +import org.apache.poi.ss.usermodel.DataFormatter;
5 import org.apache.poi.ss.usermodel.Workbook; 8 import org.apache.poi.ss.usermodel.Workbook;
6 9
7 import java.util.*; 10 import java.util.*;
@@ -11,15 +14,29 @@ import java.util.*; @@ -11,15 +14,29 @@ import java.util.*;
11 * @CreateTime: 2025-11-19 15:11 14 * @CreateTime: 2025-11-19 15:11
12 * @Version: todo 15 * @Version: todo
13 */ 16 */
  17 +@Slf4j
14 public class ExcelUtils { 18 public class ExcelUtils {
15 public static List<Map<String, Object>> processSheet(Workbook book) { 19 public static List<Map<String, Object>> processSheet(Workbook book) {
16 var res = new ArrayList<Map<String, Object>>(); 20 var res = new ArrayList<Map<String, Object>>();
17 Optional.ofNullable(book).orElseThrow(() -> new TaxAgentServiceException(TaxSystemType.ABNORMAL_PARAMETERS, "文件转换异常")); 21 Optional.ofNullable(book).orElseThrow(() -> new TaxAgentServiceException(TaxSystemType.ABNORMAL_PARAMETERS, "文件转换异常"));
  22 + var header = new HashMap<Integer, String>();
18 book.sheetIterator().forEachRemaining(sheet -> sheet.rowIterator().forEachRemaining(row -> { 23 book.sheetIterator().forEachRemaining(sheet -> sheet.rowIterator().forEachRemaining(row -> {
19 - var map = new HashMap<String, Object>();  
20 - row.cellIterator().forEachRemaining(cell -> map.put(cell.getStringCellValue(), cell.getStringCellValue()));  
21 - res.add(map); 24 + switch (row.getRowNum()) {
  25 + case 0 -> log.info("sheetName: {}", sheet.getSheetName());
  26 + case 1 -> row.cellIterator().forEachRemaining(cell -> header.put(cell.getColumnIndex(), cell.getStringCellValue()));
  27 + default -> {
  28 + var map = new HashMap<String, Object>();
  29 + row.cellIterator().forEachRemaining(cell -> map.put(header.get(cell.getColumnIndex()), getStringCellValue(cell)));
  30 + res.add(map);
  31 + }
  32 + }
22 })); 33 }));
23 return res; 34 return res;
24 } 35 }
  36 +
  37 + private static String getStringCellValue(Cell cell) {
  38 + DataFormatter formatter = new DataFormatter();
  39 + String rawValue = formatter.formatCellValue(cell); // 获取单元格显示值
  40 + return rawValue.trim().isEmpty() ? null : rawValue;
  41 + }
25 } 42 }
tax-storage/src/main/java/com/diligrp/tax/storage/controller/TaxTenantController.java
@@ -30,6 +30,7 @@ public class TaxTenantController { @@ -30,6 +30,7 @@ public class TaxTenantController {
30 30
31 @RequestMapping("call") 31 @RequestMapping("call")
32 public Message<?> call(@RequestBody Map<String, Object> map) { 32 public Message<?> call(@RequestBody Map<String, Object> map) {
  33 + map.put("value","101");
33 return Message.success(map); 34 return Message.success(map);
34 } 35 }
35 36