huminmin
2026-04-23 bc319e7fcf2c3e1d91e5de2e8c421e82b15a359e
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -240,7 +240,6 @@
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            // 固定使用 XSSFWorkbook
            XSSFWorkbook workbook = new XSSFWorkbook();
            for (SalesLedger ledger : ledgerList) {
@@ -252,15 +251,22 @@
                sheet.setColumnWidth(3, 2500);
                sheet.setColumnWidth(4, 3000);
                sheet.setColumnWidth(5, 4500);
                sheet.setColumnWidth(6, 3500);
                sheet.setColumnWidth(6, 2000);
                // 样式
                CellStyle companyTitle = createBaseStyle(workbook, "宋体", (short) 20, true, true);
                CellStyle billTitle = createBaseStyle(workbook, "宋体", (short) 18, true, true);
                CellStyle headerStyle = createBorderStyle(workbook, "宋体", (short) 12, true, true);
                CellStyle dataCenterStyle = createBorderStyle(workbook, "宋体", (short) 11, false, true);
                CellStyle noBorder = createNoBorderStyle(workbook);
                // 样式1:带虚线 + 居中
                CellStyle dashStyle = createDashBottomStyle(workbook);
                dashStyle.setAlignment(HorizontalAlignment.CENTER);
                dashStyle.setVerticalAlignment(VerticalAlignment.CENTER);
                // 样式2:无边框 + 居中(给仓库用)
                CellStyle centerNoBorder = createNoBorderStyle(workbook);
                centerNoBorder.setAlignment(HorizontalAlignment.CENTER);
                centerNoBorder.setVerticalAlignment(VerticalAlignment.CENTER);
                // 标题
                Row row0 = sheet.createRow(0);
@@ -294,81 +300,94 @@
                Row row7 = sheet.createRow(7);
                row7.createCell(0).setCellValue("货物详细信息:");
                // 表头
                int tableStart = 8;
                Row headerRow = sheet.createRow(tableStart);
                String[] headers = {"序号", "物料编号", "品名/规格", "单位", "数量", "订单号", "共四联"};
                for (int i = 0; i < headers.length; i++) {
                    Cell hc = headerRow.createCell(i);
                    hc.setCellValue(headers[i]);
                    hc.setCellStyle(i == 6 ? noBorder : headerStyle);
                    if (i == 6) {
                        hc.setCellStyle(dashStyle);
                    } else {
                        hc.setCellStyle(headerStyle);
                    }
                }
                // 查询商品
                // 查询产品
                SalesLedgerProduct cond = new SalesLedgerProduct();
                cond.setSalesLedgerId(ledger.getId());
                List<SalesLedgerProduct> productList = salesLedgerProductServiceImpl.selectSalesLedgerProductList(cond);
                BigDecimal totalQuantity = BigDecimal.ZERO;
                for (SalesLedgerProduct product : productList) {
                    totalQuantity = totalQuantity.add(product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity());
                for (SalesLedgerProduct p : productList) {
                    if (p.getQuantity() != null) totalQuantity = totalQuantity.add(p.getQuantity());
                }
                int currentRow = tableStart + 1;
                String[] fourNames = {"存根", "回单", "客户", "仓库"};
                int minDataRow = 5;
                int totalNeedRow = Math.max(minDataRow, fourNames.length);
                // 数据行 + 固定四联行
                for (int i = 0; i < totalNeedRow; i++) {
                    SalesLedgerProduct p = i < productList.size() ? productList.get(i) : null;
                    Row dataRow = sheet.createRow(currentRow);
                // 1. 所有产品
                for (int i = 0; i < productList.size(); i++) {
                    SalesLedgerProduct p = productList.get(i);
                    Row dataRow = sheet.createRow(currentRow++);
                    // 前6列赋值
                    if (p != null) {
                        String materialCode = "";
                        if (p.getProductModelId() != null) {
                            ProductModel m = productModelMapper.selectById(p.getProductModelId());
                            if (m != null) {
                                materialCode = m.getMaterialCode();
                            }
                        }
                        dataRow.createCell(0).setCellValue(i + 1);
                        dataRow.createCell(1).setCellValue(materialCode);
                        dataRow.createCell(2).setCellValue(p.getProductCategory() + "/" + p.getSpecificationModel());
                        dataRow.createCell(3).setCellValue(p.getUnit());
                        dataRow.createCell(4).setCellValue(p.getQuantity() == null ? 0 : p.getQuantity().doubleValue());
                        dataRow.createCell(5).setCellValue(ledger.getSalesContractNo());
                    } else {
                        for (int c = 0; c <= 5; c++) {
                            dataRow.createCell(c);
                        }
                    String materialCode = "";
                    if (p.getProductModelId() != null) {
                        ProductModel m = productModelMapper.selectById(p.getProductModelId());
                        if (m != null) materialCode = m.getMaterialCode();
                    }
                    // 前6列样式
                    for (int c = 0; c <= 5; c++) {
                        dataRow.getCell(c).setCellStyle(dataCenterStyle);
                    }
                    dataRow.createCell(0).setCellValue(i + 1);
                    dataRow.createCell(1).setCellValue(materialCode);
                    dataRow.createCell(2).setCellValue(p.getProductCategory() + "/" + p.getSpecificationModel());
                    dataRow.createCell(3).setCellValue(p.getUnit());
                    dataRow.createCell(4).setCellValue(p.getQuantity() == null ? 0 : p.getQuantity().doubleValue());
                    dataRow.createCell(5).setCellValue(ledger.getSalesContractNo());
                    // 共四联列
                    for (int c = 0; c <= 5; c++) dataRow.getCell(c).setCellStyle(dataCenterStyle);
                    Cell fourCell = dataRow.createCell(6);
                    if (i < fourNames.length) {
                        fourCell.setCellValue(fourNames[i]);
                        fourCell.setCellStyle(dashStyle);
                        if (i <= 2) {
                            fourCell.setCellStyle(dashStyle);
                        } else {
                            fourCell.setCellStyle(centerNoBorder);
                        }
                    } else {
                        fourCell.setCellStyle(noBorder);
                        fourCell.setCellStyle(centerNoBorder);
                    }
                    currentRow++;
                }
                // 合计行
                // 2. 不足6行补空
                int minRow = 6;
                int needEmpty = Math.max(0, minRow - productList.size());
                for (int i = 0; i < needEmpty; i++) {
                    int seq = productList.size() + i;
                    Row dataRow = sheet.createRow(currentRow++);
                    for (int c = 0; c <= 5; c++) dataRow.createCell(c).setCellStyle(dataCenterStyle);
                    Cell fourCell = dataRow.createCell(6);
                    if (seq < fourNames.length) {
                        fourCell.setCellValue(fourNames[seq]);
                        if (seq <= 2) {
                            fourCell.setCellStyle(dashStyle);
                        } else {
                            fourCell.setCellStyle(centerNoBorder);
                        }
                    } else {
                        fourCell.setCellStyle(centerNoBorder);
                    }
                }
                // 合计
                Row totalRow = sheet.createRow(currentRow);
                sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 3));
                totalRow.createCell(0).setCellValue("合计");
                totalRow.createCell(4).setCellValue(totalQuantity.doubleValue());
                fillMergeRange(sheet, currentRow, currentRow, 0, 5, dataCenterStyle);
                totalRow.createCell(6).setCellStyle(noBorder);
                totalRow.createCell(6).setCellStyle(centerNoBorder);
                // 备注
                currentRow++;
@@ -392,7 +411,7 @@
        } catch (Exception e) {
            log.error("批量导出送货单异常", e);
            throw new BaseException("批量导出失败");
            throw new RuntimeException("导出失败");
        }
    }