This commit is contained in:
van
2026-10-08 00:31:15 +08:00
parent 08d6399324
commit abac013ff6
@@ -351,6 +351,183 @@ public class RebateSheetConvertService {
return s.replace('\n', ' ').replace('\r', ' ').replace(" ", "").trim();
}
/**
* 日期按原表显示保留。文本单元格原样输出;日期单元格按自身格式输出。
* Excel 内置短日期在中文系统里是 yyyy/M/d,POI 会把它读成 m/d/yy(如 9/11/26)。
*/
private static String dateCellText(Row row, int col) {
if (row == null || col < 0) {
return "";
}
Cell cell = row.getCell(col);
if (cell == null) {
return "";
}
if (isExcelDateCell(cell)) {
return formatKeptDate(cell);
}
return cellText(row, col);
}
private static boolean isExcelDateCell(Cell cell) {
CellType type = cell.getCellType();
if (type == org.apache.poi.ss.usermodel.CellType.FORMULA) {
type = cell.getCachedFormulaResultType();
}
return type == org.apache.poi.ss.usermodel.CellType.NUMERIC && DateUtil.isCellDateFormatted(cell);
}
private static String formatKeptDate(Cell cell) {
Date date = cell.getDateCellValue();
if (date == null) {
return "";
}
short index = cell.getCellStyle() == null ? 14 : cell.getCellStyle().getDataFormat();
String fmt = cell.getCellStyle() == null ? null : cell.getCellStyle().getDataFormatString();
String pattern = javaPatternForExcelDate(index, fmt, date);
return new SimpleDateFormat(pattern, Locale.CHINA).format(date);
}
static String javaPatternForExcelDate(short index, String fmt, Date date) {
if (isLocaleShortDate(index, fmt)) {
return hasTime(date) ? "yyyy/M/d H:mm" : "yyyy/M/d";
}
String pattern = excelDateFormatToJava(fmt);
if (pattern == null) {
return hasTime(date) ? "yyyy/M/d H:mm" : "yyyy/M/d";
}
return pattern;
}
private static boolean hasTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.HOUR_OF_DAY) != 0 || cal.get(Calendar.MINUTE) != 0 || cal.get(Calendar.SECOND) != 0;
}
private static boolean isLocaleShortDate(short index, String fmt) {
if (index == 14 || index == 22) {
return true;
}
if (fmt == null) {
return false;
}
String f = fmt.replaceAll("\\[\\$-[^\\]]+\\]", "").trim().toLowerCase(Locale.ROOT);
int semi = f.indexOf(';');
if (semi >= 0) {
f = f.substring(0, semi).trim();
}
return "m/d/yy".equals(f) || "m/d/yyyy".equals(f)
|| "mm/dd/yy".equals(f) || "mm/dd/yyyy".equals(f)
|| "m/d/yy h:mm".equals(f) || "m/d/yy hh:mm".equals(f)
|| "mm/dd/yy h:mm".equals(f);
}
static String excelDateFormatToJava(String excel) {
if (excel == null) {
return null;
}
String fmt = excel.replaceAll("\\[\\$-[^\\]]+\\]", "").trim();
int semi = fmt.indexOf(';');
if (semi >= 0) {
fmt = fmt.substring(0, semi).trim();
}
if (fmt.isEmpty() || "general".equalsIgnoreCase(fmt) || "@".equals(fmt)) {
return null;
}
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < fmt.length()) {
char c = fmt.charAt(i);
if (c == '"') {
int j = fmt.indexOf('"', i + 1);
if (j < 0) {
return null;
}
sb.append('\'').append(fmt, i + 1, j).append('\'');
i = j + 1;
continue;
}
if (c == '\\' && i + 1 < fmt.length()) {
sb.append('\'').append(fmt.charAt(i + 1)).append('\'');
i += 2;
continue;
}
if (c == 'y' || c == 'Y' || c == 'd' || c == 'D') {
sb.append(Character.toLowerCase(c));
i++;
continue;
}
if (c == 'h' || c == 'H') {
sb.append('H');
i++;
continue;
}
if (c == 's' || c == 'S') {
sb.append('s');
i++;
continue;
}
if (c == 'm' || c == 'M') {
int j = i + 1;
while (j < fmt.length()) {
char n = fmt.charAt(j);
if (n != 'm' && n != 'M') {
break;
}
j++;
}
char token = minuteRun(fmt, i, j) ? 'm' : 'M';
for (int k = i; k < j; k++) {
sb.append(token);
}
i = j;
continue;
}
if ((c == 'a' || c == 'A') && fmt.regionMatches(true, i, "AM/PM", 0, 5)) {
sb.append('a');
i += 5;
continue;
}
sb.append(c);
i++;
}
String pattern = sb.toString();
try {
new SimpleDateFormat(pattern, Locale.CHINA);
return pattern;
} catch (IllegalArgumentException e) {
return null;
}
}
private static boolean minuteRun(String fmt, int start, int end) {
return timeNeighbor(fmt, start - 1, -1) || timeNeighbor(fmt, end, 1);
}
private static boolean timeNeighbor(String fmt, int index, int step) {
int i = index;
while (i >= 0 && i < fmt.length()) {
char c = fmt.charAt(i);
if (c == '"') {
int j = step > 0 ? fmt.indexOf('"', i + 1) : fmt.lastIndexOf('"', i - 1);
if (j < 0) {
return false;
}
i = j + step;
continue;
}
if (c == 'h' || c == 'H' || c == 's' || c == 'S') {
return true;
}
if (c == 'y' || c == 'Y' || c == 'd' || c == 'D' || c == 'm' || c == 'M') {
return false;
}
i += step;
}
return false;
}
private static String cellText(Row row, int col) {
if (row == null || col < 0) {
return "";