This commit is contained in:
wuweihua
2026-10-16 17:46:43 +08:00
@@ -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("<sheetData");
int sheetDataOpenEnd = templateXml.indexOf('>', sheetDataStart);
int sheetDataEnd = templateXml.indexOf("</sheetData>", 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 r=\"").append(rowNumber).append("\" ht=\"24.0\" customHeight=\"true\" spans=\"1:21\">");
row.append("<row r=\"").append(rowNumber).append("\" ht=\"")
.append(EXPORT_DETAIL_ROW_HEIGHT).append("\" customHeight=\"true\" spans=\"1:21\">");
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("</row>");