物料导入繁体解析

This commit is contained in:
秦鸿展
2026-05-29 10:02:26 +08:00
parent a7f9c5f3a9
commit 6b58811f4c
4 changed files with 295 additions and 51 deletions
@@ -0,0 +1,60 @@
package com.mhd.wms.util;
import com.mhd.common.core.utils.poi.ExcelUtil;
import com.mhd.wms.interfaces.dto.materialBaseInfo.MaterialBaseInfoDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
/**
* 物料主数据导入:支持繁体中文表头的 Excel 解析。
*/
public final class MaterialBaseInfoImportExcelUtil {
private MaterialBaseInfoImportExcelUtil() {
}
public static List<MaterialBaseInfoDTO> importRows(InputStream is) throws Exception {
Workbook workbook = WorkbookFactory.create(is);
try {
normalizeHeaderRow(workbook.getSheetAt(0), 0);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
ExcelUtil<MaterialBaseInfoDTO> util = new ExcelUtil<>(MaterialBaseInfoDTO.class);
return util.importExcel(new ByteArrayInputStream(bos.toByteArray()));
} finally {
workbook.close();
}
}
private static void normalizeHeaderRow(Sheet sheet, int titleNum) {
if (sheet == null) {
return;
}
Row headerRow = sheet.getRow(titleNum);
if (headerRow == null) {
return;
}
DataFormatter formatter = new DataFormatter();
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
Cell cell = headerRow.getCell(i);
if (cell == null) {
continue;
}
String value = formatter.formatCellValue(cell);
if (StringUtils.isBlank(value)) {
continue;
}
cell.setCellValue(ZhTextNormalizeUtil.normalizeExcelHeader(value));
}
}
}
@@ -0,0 +1,82 @@
package com.mhd.wms.util;
import com.github.houbb.opencc4j.util.ZhConverterUtil;
import org.apache.commons.lang3.StringUtils;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* 简繁体文本归一化,用于 Excel 导入表头及货主名称匹配。
*/
public final class ZhTextNormalizeUtil {
private ZhTextNormalizeUtil() {
}
public static String toSimple(String text) {
if (StringUtils.isBlank(text)) {
return text;
}
return ZhConverterUtil.toSimple(text.trim());
}
public static String toTraditional(String text) {
if (StringUtils.isBlank(text)) {
return text;
}
return ZhConverterUtil.toTraditional(text.trim());
}
/**
* 导入表头:繁体转简体,并将末尾 ID 统一为小写 id(与 @Excel name 一致)。
*/
public static String normalizeExcelHeader(String header) {
if (StringUtils.isBlank(header)) {
return header;
}
String simple = toSimple(header.trim());
if (simple.endsWith("ID")) {
return simple.substring(0, simple.length() - 2) + "id";
}
return simple;
}
public static boolean textEquals(String a, String b) {
if (a == null && b == null) {
return true;
}
if (a == null || b == null) {
return false;
}
return toSimple(a).equals(toSimple(b));
}
public static boolean textContains(String haystack, String needle) {
if (StringUtils.isBlank(haystack) || StringUtils.isBlank(needle)) {
return false;
}
return toSimple(haystack).contains(toSimple(needle));
}
/**
* 名称查询候选:原文、简体、繁体(去重,保持顺序)。
*/
public static Set<String> nameLookupVariants(String name) {
Set<String> variants = new LinkedHashSet<>();
if (StringUtils.isBlank(name)) {
return variants;
}
String trimmed = name.trim();
variants.add(trimmed);
String simple = toSimple(trimmed);
if (StringUtils.isNotBlank(simple)) {
variants.add(simple);
}
String traditional = toTraditional(trimmed);
if (StringUtils.isNotBlank(traditional)) {
variants.add(traditional);
}
return variants;
}
}