|
|
|
@ -74,8 +74,11 @@ public class ExcelTemplate { |
|
|
|
moveShape = false; |
|
|
|
moveSeal = false; |
|
|
|
cellStyle = false; |
|
|
|
cellStyle2 = false; |
|
|
|
rangeStyle = false; |
|
|
|
priceRight = false; |
|
|
|
intRight = false; |
|
|
|
delRight = false; |
|
|
|
} |
|
|
|
|
|
|
|
private boolean findAndRemoveMergedRegion(XSSFSheet sheet, int rowIndex) { |
|
|
|
@ -211,8 +214,18 @@ public class ExcelTemplate { |
|
|
|
c.setCellValue(result); // 字符串 |
|
|
|
} else { |
|
|
|
try { |
|
|
|
double num = Double.parseDouble(result.replace(",", "")); |
|
|
|
c.setCellValue(num); // 数值 |
|
|
|
// 更严格的数字检查 |
|
|
|
String cleanResult = result.replace(",", "").trim(); |
|
|
|
if (!cleanResult.isEmpty() && !cleanResult.equals("-") && !cleanResult.equals(".")) { |
|
|
|
double num = Double.parseDouble(cleanResult); |
|
|
|
if (Double.isFinite(num)) { |
|
|
|
c.setCellValue(num); // 数值 |
|
|
|
} else { |
|
|
|
c.setCellValue(result); // 字符串 |
|
|
|
} |
|
|
|
} else { |
|
|
|
c.setCellValue(result); // 字符串 |
|
|
|
} |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
c.setCellValue(result); // 字符串 |
|
|
|
} |
|
|
|
@ -223,24 +236,29 @@ public class ExcelTemplate { |
|
|
|
while (matcherDtl.find()) { |
|
|
|
for (int ri = 1; ri <= matcherDtl.groupCount(); ri++) { |
|
|
|
String field = matcherDtl.group(ri); |
|
|
|
c.setCellValue(cellValue.replace(matcherDtl.group(0), String.valueOf(listVariables.get(i - dtlRowIndex).getOrDefault(field, "")))); |
|
|
|
// 检查数组边界,避免越界异常 |
|
|
|
int listIndex = i - dtlRowIndex; |
|
|
|
if (listIndex >= 0 && listIndex < listVariables.size()) { |
|
|
|
c.setCellValue(cellValue.replace(matcherDtl.group(0), String.valueOf(listVariables.get(listIndex).getOrDefault(field, "")))); |
|
|
|
} |
|
|
|
} |
|
|
|
// 设置样式 |
|
|
|
if (cellStyle) { |
|
|
|
XSSFCellStyle style = c.getCellStyle(); |
|
|
|
style.setBorderBottom(BorderStyle.NONE); |
|
|
|
style.setBorderTop(BorderStyle.NONE); |
|
|
|
style.setWrapText(true); |
|
|
|
style.setAlignment(HorizontalAlignment.LEFT); |
|
|
|
c.setCellStyle(style); |
|
|
|
if (rangeStyle && j < 4) { |
|
|
|
for (int mi = 0; mi < 3; mi++) { |
|
|
|
XSSFCell nextc = sheet.getRow(i).getCell(c.getColumnIndex()+mi+1); |
|
|
|
if (nextc == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
nextc.setCellStyle(style); |
|
|
|
} |
|
|
|
|
|
|
|
// 设置样式 |
|
|
|
if (cellStyle && dtlRowIndex >= 0 && i >= dtlRowIndex && i < dtlRowIndex + listVariables.size()) { |
|
|
|
XSSFCellStyle style = c.getCellStyle(); |
|
|
|
style.setBorderBottom(BorderStyle.NONE); |
|
|
|
style.setBorderTop(BorderStyle.NONE); |
|
|
|
style.setWrapText(true); |
|
|
|
style.setAlignment(HorizontalAlignment.LEFT); |
|
|
|
c.setCellStyle(style); |
|
|
|
if (rangeStyle && j < 4) { |
|
|
|
for (int mi = 0; mi < 3; mi++) { |
|
|
|
XSSFCell nextc = sheet.getRow(i).getCell(c.getColumnIndex()+mi+1); |
|
|
|
if (nextc == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
nextc.setCellStyle(style); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -282,9 +300,13 @@ public class ExcelTemplate { |
|
|
|
style2.setAlignment(HorizontalAlignment.LEFT); |
|
|
|
style2.setWrapText(true); |
|
|
|
c2.setCellStyle(style2); |
|
|
|
if (priceRight) { //仅供发票excel使用,第8、9是价格列居右 |
|
|
|
if (priceRight) { //仅供发票excel使用,第6、7、8是价格列居右 |
|
|
|
for (int i = 6; i < 9; i++) { |
|
|
|
XSSFCell c7 = sheet.getRow(dtlRow).getCell(i); |
|
|
|
XSSFRow row = sheet.getRow(dtlRow); |
|
|
|
if (row == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
XSSFCell c7 = row.getCell(i); |
|
|
|
if (c7 == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
@ -326,11 +348,14 @@ public class ExcelTemplate { |
|
|
|
|
|
|
|
c7.setCellStyle(style7); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
if (intRight) { //仅供箱单excel使用 |
|
|
|
for (int i = 4; i < 9; i++) { |
|
|
|
XSSFCell c7 = sheet.getRow(dtlRow).getCell(i); |
|
|
|
XSSFRow row = sheet.getRow(dtlRow); |
|
|
|
if (row == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
XSSFCell c7 = row.getCell(i); |
|
|
|
if (c7 == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
@ -372,7 +397,6 @@ public class ExcelTemplate { |
|
|
|
} |
|
|
|
c7.setCellStyle(style7); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -380,7 +404,11 @@ public class ExcelTemplate { |
|
|
|
for (Integer dtlRow : dtlRows) { |
|
|
|
for (int i = 6; i < 12; i++) { |
|
|
|
if (i==6 || i==11) { |
|
|
|
XSSFCell c7 = sheet.getRow(dtlRow).getCell(i); |
|
|
|
XSSFRow row = sheet.getRow(dtlRow); |
|
|
|
if (row == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
XSSFCell c7 = row.getCell(i); |
|
|
|
if (c7 == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|