✨ 适配广州应收账单模板导出

This commit is contained in:
caojia
2026-11-19 17:14:25 +08:00
parent daf36e39e1
commit 5b70e70159
3 changed files with 55 additions and 23 deletions
@@ -7,6 +7,7 @@ import org.springframework.stereotype.Component;
@Component @Component
public class ArBillWorkbookWriters { public class ArBillWorkbookWriters {
public static final String SUMMARY_TEMPLATE_CODE = "AR_BILL_SUMMARY"; public static final String SUMMARY_TEMPLATE_CODE = "AR_BILL_SUMMARY";
public static final String GUANGZHOU_TEMPLATE_CODE = "AR_BILL_GZ";
private final DefaultArBillWorkbookWriter defaultWriter; private final DefaultArBillWorkbookWriter defaultWriter;
private final SummaryArBillWorkbookWriter summaryWriter; private final SummaryArBillWorkbookWriter summaryWriter;
@@ -31,7 +31,7 @@ import com.bms.receivable.domain.ArBillDetail;
import com.bms.receivable.domain.ArBillDetailFee; import com.bms.receivable.domain.ArBillDetailFee;
import com.bms.receivable.domain.ArExportTemplate; import com.bms.receivable.domain.ArExportTemplate;
/** 默认应收账单模板的 21 列明细及银行页脚排版。 */ /** 默认及广州应收账单模板共用的 21 列明细和银行页脚排版。 */
@Component @Component
public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
private static final int TEMPLATE_DATA_START_ROW = 5; private static final int TEMPLATE_DATA_START_ROW = 5;
@@ -68,7 +68,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
private static final Pattern MERGE_REF_PATTERN = Pattern.compile( private static final Pattern MERGE_REF_PATTERN = Pattern.compile(
"(<mergeCell\\b[^>]*\\bref=\")([A-Z]+)(\\d+)(?::([A-Z]+)(\\d+))?(\")"); "(<mergeCell\\b[^>]*\\bref=\")([A-Z]+)(\\d+)(?::([A-Z]+)(\\d+))?(\")");
private record ExportStyleIndexes(int failedDetail, int detailNo, int failedDetailNo, private record ExportStyleIndexes(int detail, int failedDetail, int detailNo, int failedDetailNo,
int customerOrderNo, int failedCustomerOrderNo, int customerOrderNo, int failedCustomerOrderNo,
int trackingNo, int failedTrackingNo, int trackingNo, int failedTrackingNo,
int generalNumber, int failedGeneralNumber, int generalNumber, int failedGeneralNumber,
@@ -79,18 +79,24 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
public void write(ArBill bill, ArExportTemplate template, Path target) throws IOException { public void write(ArBill bill, ArExportTemplate template, Path target) throws IOException {
// 样式表单独读取;不加载整张工作簿,且不依赖 styles.xml 和 sheet1.xml 在 ZIP 中的先后顺序。 // 样式表单独读取;不加载整张工作簿,且不依赖 styles.xml 和 sheet1.xml 在 ZIP 中的先后顺序。
StylesTable styles = readExportStyles(template); StylesTable styles = readExportStyles(template);
boolean guangzhouTemplate = ArBillWorkbookWriters.GUANGZHOU_TEMPLATE_CODE.equals(template.getTemplateCode());
// 广州模板的样式编号与默认模板不同,使用其明细合计单元格的完整边框样式。
int detailStyleIndex = guangzhouTemplate ? readGuangzhouDetailStyleIndex(template) : TEMPLATE_DETAIL_STYLE_INDEX;
if (detailStyleIndex >= styles.getNumCellStyles()) {
throw new IOException("应收账单导出模板明细样式不存在: " + detailStyleIndex);
}
String drawingPartPath = readTemplateDrawingPartPath(template); String drawingPartPath = readTemplateDrawingPartPath(template);
XSSFCellStyle detailNoStyle = styles.createCellStyle(); XSSFCellStyle detailNoStyle = styles.createCellStyle();
detailNoStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); detailNoStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex));
detailNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("0")); detailNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("0"));
int detailNoStyleIndex = Short.toUnsignedInt(detailNoStyle.getIndex()); int detailNoStyleIndex = Short.toUnsignedInt(detailNoStyle.getIndex());
XSSFCellStyle failedDetailStyle = styles.createCellStyle(); XSSFCellStyle failedDetailStyle = styles.createCellStyle();
failedDetailStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); failedDetailStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex));
failedDetailStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); failedDetailStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
failedDetailStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); failedDetailStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
int failedDetailStyleIndex = Short.toUnsignedInt(failedDetailStyle.getIndex()); int failedDetailStyleIndex = Short.toUnsignedInt(failedDetailStyle.getIndex());
XSSFCellStyle customerOrderNoStyle = styles.createCellStyle(); XSSFCellStyle customerOrderNoStyle = styles.createCellStyle();
customerOrderNoStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); customerOrderNoStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex));
customerOrderNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@")); customerOrderNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@"));
int customerOrderNoStyleIndex = Short.toUnsignedInt(customerOrderNoStyle.getIndex()); int customerOrderNoStyleIndex = Short.toUnsignedInt(customerOrderNoStyle.getIndex());
XSSFCellStyle failedCustomerOrderNoStyle = styles.createCellStyle(); XSSFCellStyle failedCustomerOrderNoStyle = styles.createCellStyle();
@@ -98,7 +104,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
failedCustomerOrderNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@")); failedCustomerOrderNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@"));
int failedCustomerOrderNoStyleIndex = Short.toUnsignedInt(failedCustomerOrderNoStyle.getIndex()); int failedCustomerOrderNoStyleIndex = Short.toUnsignedInt(failedCustomerOrderNoStyle.getIndex());
XSSFCellStyle trackingNoStyle = styles.createCellStyle(); XSSFCellStyle trackingNoStyle = styles.createCellStyle();
trackingNoStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); trackingNoStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex));
trackingNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@")); trackingNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@"));
int trackingNoStyleIndex = Short.toUnsignedInt(trackingNoStyle.getIndex()); int trackingNoStyleIndex = Short.toUnsignedInt(trackingNoStyle.getIndex());
XSSFCellStyle failedTrackingNoStyle = styles.createCellStyle(); XSSFCellStyle failedTrackingNoStyle = styles.createCellStyle();
@@ -106,7 +112,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
failedTrackingNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@")); failedTrackingNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@"));
int failedTrackingNoStyleIndex = Short.toUnsignedInt(failedTrackingNoStyle.getIndex()); int failedTrackingNoStyleIndex = Short.toUnsignedInt(failedTrackingNoStyle.getIndex());
XSSFCellStyle generalNumberStyle = styles.createCellStyle(); XSSFCellStyle generalNumberStyle = styles.createCellStyle();
generalNumberStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); generalNumberStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex));
generalNumberStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("General")); generalNumberStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("General"));
int generalNumberStyleIndex = Short.toUnsignedInt(generalNumberStyle.getIndex()); int generalNumberStyleIndex = Short.toUnsignedInt(generalNumberStyle.getIndex());
XSSFCellStyle failedGeneralNumberStyle = styles.createCellStyle(); XSSFCellStyle failedGeneralNumberStyle = styles.createCellStyle();
@@ -118,7 +124,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
failedDetailNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("0")); failedDetailNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("0"));
int failedDetailNoStyleIndex = Short.toUnsignedInt(failedDetailNoStyle.getIndex()); int failedDetailNoStyleIndex = Short.toUnsignedInt(failedDetailNoStyle.getIndex());
XSSFCellStyle formulaStyle = styles.createCellStyle(); XSSFCellStyle formulaStyle = styles.createCellStyle();
formulaStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); formulaStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex));
formulaStyle.setShrinkToFit(true); formulaStyle.setShrinkToFit(true);
int formulaStyleIndex = Short.toUnsignedInt(formulaStyle.getIndex()); int formulaStyleIndex = Short.toUnsignedInt(formulaStyle.getIndex());
XSSFCellStyle failedFormulaStyle = styles.createCellStyle(); XSSFCellStyle failedFormulaStyle = styles.createCellStyle();
@@ -126,7 +132,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
failedFormulaStyle.setShrinkToFit(true); failedFormulaStyle.setShrinkToFit(true);
int failedFormulaStyleIndex = Short.toUnsignedInt(failedFormulaStyle.getIndex()); int failedFormulaStyleIndex = Short.toUnsignedInt(failedFormulaStyle.getIndex());
ExportStyleIndexes exportStyleIndexes = new ExportStyleIndexes( ExportStyleIndexes exportStyleIndexes = new ExportStyleIndexes(
failedDetailStyleIndex, detailNoStyleIndex, failedDetailNoStyleIndex, detailStyleIndex, failedDetailStyleIndex, detailNoStyleIndex, failedDetailNoStyleIndex,
customerOrderNoStyleIndex, failedCustomerOrderNoStyleIndex, customerOrderNoStyleIndex, failedCustomerOrderNoStyleIndex,
trackingNoStyleIndex, failedTrackingNoStyleIndex, trackingNoStyleIndex, failedTrackingNoStyleIndex,
generalNumberStyleIndex, failedGeneralNumberStyleIndex, generalNumberStyleIndex, failedGeneralNumberStyleIndex,
@@ -151,7 +157,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
} }
if (TEMPLATE_SHEET_XML.equals(entry.getName())) { if (TEMPLATE_SHEET_XML.equals(entry.getName())) {
output.putNextEntry(new ZipEntry(entryName)); output.putNextEntry(new ZipEntry(entryName));
int rowOffset = writeOoxmlSheet(readUtf8(templateZip), bill, output, exportStyleIndexes); int rowOffset = writeOoxmlSheet(readUtf8(templateZip), bill, output, exportStyleIndexes, guangzhouTemplate);
output.closeEntry(); output.closeEntry();
replaced = true; replaced = true;
drawingRowOffset = Integer.valueOf(rowOffset); drawingRowOffset = Integer.valueOf(rowOffset);
@@ -178,6 +184,29 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
} }
} }
private int readGuangzhouDetailStyleIndex(ArExportTemplate template) throws IOException {
try (InputStream input = openTemplateStream(template); ZipInputStream zip = new ZipInputStream(input)) {
for (ZipEntry entry; (entry = zip.getNextEntry()) != null; ) {
if (TEMPLATE_SHEET_XML.equals(entry.getName())) {
String row = extractTemplateRows(readUtf8(zip)).get(6);
if (row != null) {
Matcher cells = CELL_PATTERN.matcher(row);
while (cells.find()) {
Matcher reference = CELL_REF_PATTERN.matcher(cells.group(1));
Matcher style = CELL_STYLE_PATTERN.matcher(cells.group(1));
if (reference.find() && "S6".equalsIgnoreCase(reference.group(1)) && style.find()) {
return Integer.parseInt(style.group(1));
}
}
}
throw new IOException("广州应收账单模板缺少 S6 合计单元格样式");
}
zip.closeEntry();
}
}
throw new IOException("广州应收账单模板缺少运费工作表");
}
private String readTemplateDrawingPartPath(ArExportTemplate template) throws IOException { private String readTemplateDrawingPartPath(ArExportTemplate template) throws IOException {
try (InputStream input = openTemplateStream(template); ZipInputStream templateZip = new ZipInputStream(input)) { try (InputStream input = openTemplateStream(template); ZipInputStream templateZip = new ZipInputStream(input)) {
for (ZipEntry entry; (entry = templateZip.getNextEntry()) != null; ) { for (ZipEntry entry; (entry = templateZip.getNextEntry()) != null; ) {
@@ -236,11 +265,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
try (InputStream input = openTemplateStream(template); ZipInputStream templateZip = new ZipInputStream(input)) { try (InputStream input = openTemplateStream(template); ZipInputStream templateZip = new ZipInputStream(input)) {
for (ZipEntry entry; (entry = templateZip.getNextEntry()) != null; ) { for (ZipEntry entry; (entry = templateZip.getNextEntry()) != null; ) {
if (TEMPLATE_STYLES_XML.equals(entry.getName())) { if (TEMPLATE_STYLES_XML.equals(entry.getName())) {
StylesTable styles = new StylesTable(templateZip); return new StylesTable(templateZip);
if (styles.getNumCellStyles() <= TEMPLATE_DETAIL_STYLE_INDEX) {
throw new IOException("应收账单导出模板缺少明细样式: " + TEMPLATE_DETAIL_STYLE_INDEX);
}
return styles;
} }
templateZip.closeEntry(); templateZip.closeEntry();
} }
@@ -249,7 +274,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
} }
private int writeOoxmlSheet(String templateXml, ArBill bill, OutputStream output, private int writeOoxmlSheet(String templateXml, ArBill bill, OutputStream output,
ExportStyleIndexes styleIndexes) throws IOException { ExportStyleIndexes styleIndexes, boolean guangzhouTemplate) throws IOException {
int sheetDataStart = templateXml.indexOf("<sheetData"); int sheetDataStart = templateXml.indexOf("<sheetData");
int sheetDataOpenEnd = templateXml.indexOf('>', sheetDataStart); int sheetDataOpenEnd = templateXml.indexOf('>', sheetDataStart);
int sheetDataEnd = templateXml.indexOf("</sheetData>", sheetDataOpenEnd); int sheetDataEnd = templateXml.indexOf("</sheetData>", sheetDataOpenEnd);
@@ -263,7 +288,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
removeSheetDimension(templateXml.substring(0, sheetDataOpenEnd + 1))); removeSheetDimension(templateXml.substring(0, sheetDataOpenEnd + 1)));
writeUtf8(output, prefix); writeUtf8(output, prefix);
String headerRow = replaceCellXml(templateRows.get(1), "A1", String headerRow = guangzhouTemplate ? templateRows.get(1) : replaceCellXml(templateRows.get(1), "A1",
StringUtils.isEmpty(bill.getBankAccountName()) ? "威狮国际供应链(深圳)有限公司" : bill.getBankAccountName(), false); StringUtils.isEmpty(bill.getBankAccountName()) ? "威狮国际供应链(深圳)有限公司" : bill.getBankAccountName(), false);
writeUtf8(output, headerRow); writeUtf8(output, headerRow);
writeUtf8(output, templateRows.get(2)); writeUtf8(output, templateRows.get(2));
@@ -291,7 +316,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
int rowOffset = lastDetailRow - TEMPLATE_LAST_DETAIL_ROW + EXPORT_FOOTER_GAP_ROWS; int rowOffset = lastDetailRow - TEMPLATE_LAST_DETAIL_ROW + EXPORT_FOOTER_GAP_ROWS;
writeUtf8(output, "<row r=\"" + (lastDetailRow + 1) + "\" ht=\"" writeUtf8(output, "<row r=\"" + (lastDetailRow + 1) + "\" ht=\""
+ EXPORT_DETAIL_ROW_HEIGHT + "\" customHeight=\"true\"/>"); + EXPORT_DETAIL_ROW_HEIGHT + "\" customHeight=\"true\"/>");
writeOoxmlFooter(output, templateRows, rowOffset, bill, styleIndexes.generalNumber()); writeOoxmlFooter(output, templateRows, rowOffset, bill, styleIndexes.generalNumber(), guangzhouTemplate);
String suffix = shiftFooterMerges(templateXml.substring(sheetDataEnd), rowOffset); String suffix = shiftFooterMerges(templateXml.substring(sheetDataEnd), rowOffset);
writeUtf8(output, suffix); writeUtf8(output, suffix);
return rowOffset; return rowOffset;
@@ -394,7 +419,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
} }
private void writeOoxmlFooter(OutputStream output, Map<Integer, String> templateRows, int rowOffset, private void writeOoxmlFooter(OutputStream output, Map<Integer, String> templateRows, int rowOffset,
ArBill bill, int generalNumberStyleIndex) throws IOException { ArBill bill, int generalNumberStyleIndex, boolean guangzhouTemplate) throws IOException {
String totalAmountFormula = "SUM(S" + TEMPLATE_DATA_START_ROW + ":INDEX(S:S,ROW()-1))"; String totalAmountFormula = "SUM(S" + TEMPLATE_DATA_START_ROW + ":INDEX(S:S,ROW()-1))";
for (int offset = 0; offset < 4; offset++) { for (int offset = 0; offset < 4; offset++) {
int sourceRowNumber = TEMPLATE_LAST_DETAIL_ROW + offset + 1; int sourceRowNumber = TEMPLATE_LAST_DETAIL_ROW + offset + 1;
@@ -407,20 +432,26 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
nvl(bill.getTotalAmount()), generalNumberStyleIndex); nvl(bill.getTotalAmount()), generalNumberStyleIndex);
} else if (offset == 1) { } else if (offset == 1) {
row = replaceCellXml(row, "B" + targetRowNumber, "开户名", false); row = replaceCellXml(row, "B" + targetRowNumber, "开户名", false);
row = replaceCellXml(row, "C" + targetRowNumber, bill.getBankAccountName(), false); if (!guangzhouTemplate) {
row = replaceCellXml(row, "C" + targetRowNumber, bill.getBankAccountName(), false);
}
row = replaceCellXml(row, "P" + targetRowNumber, "本期已核销金额", false); row = replaceCellXml(row, "P" + targetRowNumber, "本期已核销金额", false);
row = replaceCellXml(row, "S" + targetRowNumber, bill.getWrittenOffAmount(), true, row = replaceCellXml(row, "S" + targetRowNumber, bill.getWrittenOffAmount(), true,
generalNumberStyleIndex); generalNumberStyleIndex);
} else if (offset == 2) { } else if (offset == 2) {
row = replaceCellXml(row, "B" + targetRowNumber, "账号", false); row = replaceCellXml(row, "B" + targetRowNumber, "账号", false);
row = replaceCellXml(row, "C" + targetRowNumber, exportBankAccountNo(bill.getBankAccountNo()), false); if (!guangzhouTemplate) {
row = replaceCellXml(row, "C" + targetRowNumber, exportBankAccountNo(bill.getBankAccountNo()), false);
}
row = replaceCellXml(row, "P" + targetRowNumber, "本期未核销金额", false); row = replaceCellXml(row, "P" + targetRowNumber, "本期未核销金额", false);
String unwrittenAmountFormula = "S" + (targetRowNumber - 2) + "-S" + (targetRowNumber - 1); String unwrittenAmountFormula = "S" + (targetRowNumber - 2) + "-S" + (targetRowNumber - 1);
row = replaceFormulaCellXml(row, "S" + targetRowNumber, unwrittenAmountFormula, row = replaceFormulaCellXml(row, "S" + targetRowNumber, unwrittenAmountFormula,
nvl(bill.getUnwrittenAmount()), generalNumberStyleIndex); nvl(bill.getUnwrittenAmount()), generalNumberStyleIndex);
} else { } else {
row = replaceCellXml(row, "B" + targetRowNumber, "开户行", false); row = replaceCellXml(row, "B" + targetRowNumber, "开户行", false);
row = replaceCellXml(row, "C" + targetRowNumber, bill.getBankName(), false); if (!guangzhouTemplate) {
row = replaceCellXml(row, "C" + targetRowNumber, bill.getBankName(), false);
}
} }
writeUtf8(output, row); writeUtf8(output, row);
} }
@@ -430,7 +461,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
ExportStyleIndexes styleIndexes) throws IOException { ExportStyleIndexes styleIndexes) throws IOException {
String failureDescription = exportBillingFailDescription(detail.getBillingFailStatus()); String failureDescription = exportBillingFailDescription(detail.getBillingFailStatus());
boolean failedDetail = !failureDescription.isEmpty(); boolean failedDetail = !failureDescription.isEmpty();
int styleIndex = failedDetail ? styleIndexes.failedDetail() : TEMPLATE_DETAIL_STYLE_INDEX; int styleIndex = failedDetail ? styleIndexes.failedDetail() : styleIndexes.detail();
int sequenceStyleIndex = failedDetail ? styleIndexes.failedDetailNo() : styleIndexes.detailNo(); int sequenceStyleIndex = failedDetail ? styleIndexes.failedDetailNo() : styleIndexes.detailNo();
Object[] values = { Object[] values = {
detail.getDetailNo(), detail.getDetailNo(),