diff --git a/bms-back/bms-system/src/main/java/com/bms/ar/service/impl/ArBillServiceImpl.java b/bms-back/bms-system/src/main/java/com/bms/ar/service/impl/ArBillServiceImpl.java index 49bb55f9..f18758ac 100644 --- a/bms-back/bms-system/src/main/java/com/bms/ar/service/impl/ArBillServiceImpl.java +++ b/bms-back/bms-system/src/main/java/com/bms/ar/service/impl/ArBillServiceImpl.java @@ -31,6 +31,7 @@ import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFFont; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.core.io.ClassPathResource; @@ -113,6 +114,7 @@ public class ArBillServiceImpl implements IArBillService { private static final int TEMPLATE_DATA_START_ROW = 5; private static final int TEMPLATE_FOOTER_START_ROW = 5; private static final int TEMPLATE_COLUMN_COUNT = 21; + private static final double EXPORT_DETAIL_ROW_HEIGHT = 20.0; /** 客户订单号、运单号位于 Excel 的 C、D 列,按给定样例长度并额外保留两字符边距。 */ private static final int CUSTOMER_ORDER_NO_COLUMN_INDEX = 3; private static final int TRACKING_NO_COLUMN_INDEX = 4; @@ -121,7 +123,7 @@ public class ArBillServiceImpl implements IArBillService { private static final double MIN_CUSTOMER_ORDER_NO_COLUMN_WIDTH = 20.0; private static final double MIN_TRACKING_NO_COLUMN_WIDTH = 35.0; private static final double MIN_FORMULA_COLUMN_WIDTH = 25.0; - private static final double MIN_FEE_TYPE_COLUMN_WIDTH = 25.0; + private static final double MIN_FEE_TYPE_COLUMN_WIDTH = 10.0; /** * 单次查询订单数;费用仅查询当前订单批次,避免大账单占满堆内存。 */ @@ -1305,6 +1307,18 @@ public class ArBillServiceImpl implements IArBillService { failedDetailNoStyle.cloneStyleFrom(failedDetailStyle); failedDetailNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("0")); int failedDetailNoStyleIndex = Short.toUnsignedInt(failedDetailNoStyle.getIndex()); + XSSFFont formulaFont = new XSSFFont(); + formulaFont.getCTFont().set(styles.getFontAt( + styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX).getFontIndexAsInt()).getCTFont()); + formulaFont.setFontHeightInPoints((short) 9); + XSSFCellStyle formulaStyle = styles.createCellStyle(); + formulaStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); + formulaStyle.setFont(formulaFont); + int formulaStyleIndex = Short.toUnsignedInt(formulaStyle.getIndex()); + XSSFCellStyle failedFormulaStyle = styles.createCellStyle(); + failedFormulaStyle.cloneStyleFrom(failedDetailStyle); + failedFormulaStyle.setFont(formulaFont); + int failedFormulaStyleIndex = Short.toUnsignedInt(failedFormulaStyle.getIndex()); try (InputStream input = openTemplateStream(template); ZipInputStream templateZip = new ZipInputStream(input); OutputStream fileOutput = Files.newOutputStream(target); ZipOutputStream output = new ZipOutputStream(fileOutput)) { boolean replaced = false; @@ -1313,7 +1327,7 @@ public class ArBillServiceImpl implements IArBillService { output.putNextEntry(outputEntry); if (TEMPLATE_SHEET_XML.equals(entry.getName())) { writeOoxmlSheet(readUtf8(templateZip), bill, output, failedDetailStyleIndex, - detailNoStyleIndex, failedDetailNoStyleIndex); + detailNoStyleIndex, failedDetailNoStyleIndex, formulaStyleIndex, failedFormulaStyleIndex); replaced = true; } else if (TEMPLATE_STYLES_XML.equals(entry.getName())) { styles.writeTo(output); @@ -1373,7 +1387,8 @@ public class ArBillServiceImpl implements IArBillService { * 以 XML 流方式写入表头、分页明细和银行信息,避免 POI 在大数据量下移动行。 */ private void writeOoxmlSheet(String templateXml, ArBill bill, OutputStream output, int failedDetailStyleIndex, - int detailNoStyleIndex, int failedDetailNoStyleIndex) throws IOException { + int detailNoStyleIndex, int failedDetailNoStyleIndex, + int formulaStyleIndex, int failedFormulaStyleIndex) throws IOException { int sheetDataStart = templateXml.indexOf("', sheetDataStart); int sheetDataEnd = templateXml.indexOf("", sheetDataOpenEnd); @@ -1407,7 +1422,7 @@ public class ArBillServiceImpl implements IArBillService { attachFees(details); for (ArBillDetail detail : details) { writeOoxmlDetailRow(output, rowIndex++, detail, failedDetailStyleIndex, - detailNoStyleIndex, failedDetailNoStyleIndex); + detailNoStyleIndex, failedDetailNoStyleIndex, formulaStyleIndex, failedFormulaStyleIndex); } lastDetailNo = details.get(details.size() - 1).getDetailNo(); } @@ -1552,7 +1567,8 @@ public class ArBillServiceImpl implements IArBillService { } private void writeOoxmlDetailRow(OutputStream output, int rowNumber, ArBillDetail detail, int failedDetailStyleIndex, - int detailNoStyleIndex, int failedDetailNoStyleIndex) throws IOException { + int detailNoStyleIndex, int failedDetailNoStyleIndex, + int formulaStyleIndex, int failedFormulaStyleIndex) throws IOException { String failureDescription = exportBillingFailDescription(detail.getBillingFailStatus()); boolean failedDetail = !failureDescription.isEmpty(); int styleIndex = failedDetail ? failedDetailStyleIndex : TEMPLATE_DETAIL_STYLE_INDEX; @@ -1570,9 +1586,12 @@ public class ArBillServiceImpl implements IArBillService { failureDescription }; StringBuilder row = new StringBuilder(2048); - row.append(""); + row.append(""); for (int index = 0; index < TEMPLATE_COLUMN_COUNT; index++) { - int cellStyleIndex = index == 0 ? sequenceStyleIndex : styleIndex; + int cellStyleIndex = index == 0 ? sequenceStyleIndex + : index == FORMULA_COLUMN_INDEX - 1 + ? (failedDetail ? failedFormulaStyleIndex : formulaStyleIndex) : styleIndex; appendOoxmlCell(row, (char) ('A' + index) + String.valueOf(rowNumber), values[index], cellStyleIndex); } row.append("");