|
|
|
@ -34,9 +34,15 @@ public class ExcelTemplate { |
|
|
|
// 是否设置合并单元格样式 |
|
|
|
@Setter |
|
|
|
private boolean rangeStyle = false; |
|
|
|
// 价格靠右 |
|
|
|
// 价格靠右 发票 |
|
|
|
@Setter |
|
|
|
private boolean priceRight = false; |
|
|
|
// 数字靠右 箱单 |
|
|
|
@Setter |
|
|
|
private boolean intRight = false; |
|
|
|
// 报关单 |
|
|
|
@Setter |
|
|
|
private boolean delRight = false; |
|
|
|
|
|
|
|
private ExcelTemplate(){} |
|
|
|
|
|
|
|
@ -190,14 +196,29 @@ public class ExcelTemplate { |
|
|
|
if ("#{pn}".equals(cellValue)) { |
|
|
|
dtlRows.add(i); |
|
|
|
} |
|
|
|
if ("#{levy}".equals(cellValue)) { |
|
|
|
dtlRows.add(i); |
|
|
|
} |
|
|
|
Matcher matcherHdr = PATTERN_HDR_FIELD.matcher(cellValue); |
|
|
|
String result = c.getStringCellValue(); |
|
|
|
while (matcherHdr.find()) { |
|
|
|
for (int ri = 1; ri <= matcherHdr.groupCount(); ri++) { |
|
|
|
String field = matcherHdr.group(ri); |
|
|
|
//all cell in string now, can be set to different type of values |
|
|
|
c.setCellValue(c.toString().replace(matcherHdr.group(0), String.valueOf(variables.getOrDefault(field, "")))); |
|
|
|
Object value = variables.getOrDefault(field, ""); |
|
|
|
result = result.replace(matcherHdr.group(0), String.valueOf(value)); |
|
|
|
} |
|
|
|
if ("${phone1}".equals(cellValue) || "${phone2}".equals(cellValue) || "${hs_code}".equals(cellValue)) { |
|
|
|
c.setCellValue(result); // 字符串 |
|
|
|
} else { |
|
|
|
try { |
|
|
|
double num = Double.parseDouble(result.replace(",", "")); |
|
|
|
c.setCellValue(num); // 数值 |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
c.setCellValue(result); // 字符串 |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Matcher matcherDtl = PATTERN_DTL_FIELD.matcher(cellValue); |
|
|
|
while (matcherDtl.find()) { |
|
|
|
for (int ri = 1; ri <= matcherDtl.groupCount(); ri++) { |
|
|
|
@ -262,22 +283,130 @@ public class ExcelTemplate { |
|
|
|
style2.setWrapText(true); |
|
|
|
c2.setCellStyle(style2); |
|
|
|
if (priceRight) { //仅供发票excel使用,第8、9是价格列居右 |
|
|
|
for (int i = 7; i < 9; i++) { |
|
|
|
for (int i = 6; i < 9; i++) { |
|
|
|
XSSFCell c7 = sheet.getRow(dtlRow).getCell(i); |
|
|
|
if (c7 == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// 尝试把字符串转成数值 |
|
|
|
if (c7.getCellType() == CellType.STRING) { |
|
|
|
String strVal = c7.getStringCellValue(); |
|
|
|
if (strVal != null && !strVal.trim().isEmpty()) { |
|
|
|
try { |
|
|
|
double num = Double.parseDouble(strVal.replace(",", "")); |
|
|
|
c7.setCellValue(num); // 转换为数值写回 |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
// 如果不是数字就保留原字符串 |
|
|
|
System.out.println("非数字,保持原值: " + strVal); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 创建样式 |
|
|
|
XSSFCellStyle style7 = workbook.createCellStyle(); |
|
|
|
style7.setBorderRight(BorderStyle.MEDIUM); |
|
|
|
style7.setBorderLeft(BorderStyle.NONE); |
|
|
|
style7.setBorderBottom(BorderStyle.NONE); |
|
|
|
style7.setBorderTop(BorderStyle.NONE); |
|
|
|
|
|
|
|
Font font7 = workbook.createFont(); |
|
|
|
font7.setFontName("Arial"); // 设置字体 |
|
|
|
font7.setFontHeightInPoints((short) 10); // 设置字号 |
|
|
|
font7.setFontName("Arial"); |
|
|
|
font7.setFontHeightInPoints((short) 10); |
|
|
|
style7.setFont(font7); |
|
|
|
|
|
|
|
style7.setVerticalAlignment(VerticalAlignment.TOP); |
|
|
|
style7.setAlignment(HorizontalAlignment.RIGHT); |
|
|
|
|
|
|
|
// 设置千分位格式 |
|
|
|
DataFormat dataFormat = workbook.createDataFormat(); |
|
|
|
style7.setDataFormat(i==6?dataFormat.getFormat("#,##0.0"):dataFormat.getFormat("#,##0.00")); |
|
|
|
|
|
|
|
c7.setCellStyle(style7); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
if (intRight) { //仅供箱单excel使用 |
|
|
|
for (int i = 4; i < 9; i++) { |
|
|
|
XSSFCell c7 = sheet.getRow(dtlRow).getCell(i); |
|
|
|
if (c7 == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
// 尝试把字符串转成数值 |
|
|
|
if (c7.getCellType() == CellType.STRING) { |
|
|
|
String strVal = c7.getStringCellValue(); |
|
|
|
if (strVal != null && !strVal.trim().isEmpty()) { |
|
|
|
try { |
|
|
|
double num = Double.parseDouble(strVal.replace(",", "")); |
|
|
|
c7.setCellValue(num); // 转换为数值写回 |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
// 如果不是数字就保留原字符串 |
|
|
|
System.out.println("非数字,保持原值: " + strVal); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 创建样式 |
|
|
|
XSSFCellStyle style7 = workbook.createCellStyle(); |
|
|
|
style7.setBorderRight(BorderStyle.MEDIUM); |
|
|
|
style7.setBorderLeft(i==4?BorderStyle.MEDIUM:BorderStyle.NONE); |
|
|
|
style7.setBorderBottom(BorderStyle.NONE); |
|
|
|
style7.setBorderTop(BorderStyle.NONE); |
|
|
|
|
|
|
|
Font font7 = workbook.createFont(); |
|
|
|
font7.setFontName("Arial"); |
|
|
|
font7.setFontHeightInPoints((short) 10); |
|
|
|
style7.setFont(font7); |
|
|
|
|
|
|
|
style7.setVerticalAlignment(VerticalAlignment.TOP); |
|
|
|
style7.setAlignment(HorizontalAlignment.RIGHT); |
|
|
|
|
|
|
|
// 设置千分位格式 |
|
|
|
DataFormat dataFormat = workbook.createDataFormat(); |
|
|
|
style7.setDataFormat(dataFormat.getFormat("#,##0.0")); |
|
|
|
|
|
|
|
c7.setCellStyle(style7); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (delRight) { |
|
|
|
for (Integer dtlRow : dtlRows) { |
|
|
|
for (int i = 6; i < 12; i++) { |
|
|
|
if (i==6 || i==11) { |
|
|
|
XSSFCell c7 = sheet.getRow(dtlRow).getCell(i); |
|
|
|
if (c7 == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
// 尝试把字符串转成数值 |
|
|
|
if (c7.getCellType() == CellType.STRING) { |
|
|
|
String strVal = c7.getStringCellValue(); |
|
|
|
if (strVal != null && !strVal.trim().isEmpty()) { |
|
|
|
try { |
|
|
|
double num = Double.parseDouble(strVal.replace(",", "")); |
|
|
|
c7.setCellValue(num); // 转换为数值写回 |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
// 如果不是数字就保留原字符串 |
|
|
|
System.out.println("非数字,保持原值: " + strVal); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// 创建样式 |
|
|
|
XSSFCellStyle style7 = workbook.createCellStyle(); |
|
|
|
style7.setBorderRight(BorderStyle.NONE); |
|
|
|
style7.setBorderLeft(BorderStyle.NONE); |
|
|
|
style7.setBorderBottom(BorderStyle.NONE); |
|
|
|
style7.setBorderTop(BorderStyle.NONE); |
|
|
|
Font font7 = workbook.createFont(); |
|
|
|
font7.setFontName("Arial"); |
|
|
|
font7.setFontHeightInPoints((short) 10); |
|
|
|
style7.setFont(font7); |
|
|
|
style7.setVerticalAlignment(VerticalAlignment.CENTER); |
|
|
|
style7.setAlignment(HorizontalAlignment.RIGHT); |
|
|
|
// 设置千分位格式 |
|
|
|
DataFormat dataFormat = workbook.createDataFormat(); |
|
|
|
style7.setDataFormat(i==6?dataFormat.getFormat("#,##0.0"):dataFormat.getFormat("#,##0.00")); |
|
|
|
c7.setCellStyle(style7); |
|
|
|
} |
|
|
|
} |
|
|
|
|