Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54c0e420bf | ||
|
|
abac013ff6 | ||
|
|
08d6399324 |
+183
-1
@@ -8,6 +8,7 @@ import com.ruoyi.jarvis.util.QuickRecordModelShopOptionUtil;
|
||||
import org.apache.poi.ss.usermodel.BorderStyle;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.CellType;
|
||||
import org.apache.poi.ss.usermodel.DataFormatter;
|
||||
import org.apache.poi.ss.usermodel.DateUtil;
|
||||
import org.apache.poi.ss.usermodel.FillPatternType;
|
||||
@@ -26,10 +27,14 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -262,7 +267,7 @@ public class RebateSheetConvertService {
|
||||
continue;
|
||||
}
|
||||
SourceRow src = new SourceRow();
|
||||
src.dateText = cellText(row, header.dateCol);
|
||||
src.dateText = dateCellText(row, header.dateCol);
|
||||
src.orderNo = normalizeOrderNo(cellText(row, header.orderCol));
|
||||
src.model = cellText(row, header.modelCol);
|
||||
String rebateText = firstNonEmpty(cellText(row, header.totalRebateCol), cellText(row, header.rebateCol));
|
||||
@@ -347,6 +352,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 == CellType.FORMULA) {
|
||||
type = cell.getCachedFormulaResultType();
|
||||
}
|
||||
return type == 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 "";
|
||||
|
||||
Reference in New Issue
Block a user