From 16c1cb7098f15b559ff02eedabd41b9eefd20ad9 Mon Sep 17 00:00:00 2001
From: buhuazhen <hua100783@gmail.com>
Date: 星期四, 21 五月 2026 16:55:24 +0800
Subject: [PATCH]  feat: 报工补充加放数

---
 src/main/java/com/ruoyi/basic/excel/ProductionPrintOrderExcel.java |  127 ++++++++++++++++++++++++++++++++++++------
 1 files changed, 108 insertions(+), 19 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/excel/ProductionPrintOrderExcel.java b/src/main/java/com/ruoyi/basic/excel/ProductionPrintOrderExcel.java
index 29f2726..e8bdd25 100644
--- a/src/main/java/com/ruoyi/basic/excel/ProductionPrintOrderExcel.java
+++ b/src/main/java/com/ruoyi/basic/excel/ProductionPrintOrderExcel.java
@@ -1,7 +1,7 @@
 package com.ruoyi.basic.excel;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.io.FileUtil;
-import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.IdUtil;
 import com.alibaba.excel.EasyExcel;
 import com.alibaba.excel.ExcelWriter;
@@ -11,19 +11,14 @@
 import com.alibaba.excel.write.metadata.fill.FillWrapper;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.production.dto.ExportProductionPrintOrderDto;
-import com.ruoyi.production.pojo.ProductionPrintOrder;
 import lombok.SneakyThrows;
 import org.apache.poi.ss.usermodel.*;
-import org.apache.poi.xssf.usermodel.XSSFDrawing;
-import org.apache.poi.xssf.usermodel.XSSFPicture;
-import org.apache.poi.xssf.usermodel.XSSFShape;
-import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.ss.util.CellRangeAddress;
 import org.springframework.stereotype.Component;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
-import java.util.List;
 
 /**
  * @author buhuazhen
@@ -34,8 +29,65 @@
 public class ProductionPrintOrderExcel {
 
 
+    /**
+     * 鍚堝苟鍗曞厓鏍煎苟涓哄悎骞跺尯鍩熸坊鍔犲乏鍙宠竟妗�
+     *
+     * @param sheet    Sheet
+     * @param workbook Workbook
+     * @param range    瑕佸悎骞剁殑鍖哄煙
+     */
+    public static void mergeWithBorder(Sheet sheet, Workbook workbook, CellRangeAddress range, CellStyle s) {
+
+        // 1. 娣诲姞鍚堝苟鍖哄煙锛堟娴嬫槸鍚﹂噸鍙狅級
+        for (int i = sheet.getNumMergedRegions() - 1; i >= 0; i--) {
+            CellRangeAddress old = sheet.getMergedRegion(i);
+            // 鍒ゆ柇鏄惁閲嶅彔锛堢畝鍗曠煩褰㈤噸鍙犻�昏緫锛�
+            if (!(range.getLastRow() < old.getFirstRow()
+                    || range.getFirstRow() > old.getLastRow()
+                    || range.getLastColumn() < old.getFirstColumn()
+                    || range.getFirstColumn() > old.getLastColumn())) {
+
+                // 瀛樺湪閲嶅彔 鈫� 鍒犻櫎鏃х殑
+                sheet.removeMergedRegion(i);
+            }
+        }
+
+        sheet.addMergedRegion(range);
+
+        // 2. 涓哄悎骞跺尯鍩熷唴鐨勬墍鏈夊崟鍏冩牸娣诲姞宸﹀彸杈规
+        for (int rowIdx = range.getFirstRow(); rowIdx <= range.getLastRow(); rowIdx++) {
+            Row row = sheet.getRow(rowIdx);
+            if (row == null) {
+                row = sheet.createRow(rowIdx);
+            }
+
+            for (int colIdx = range.getFirstColumn(); colIdx <= range.getLastColumn(); colIdx++) {
+
+                Cell cell = row.getCell(colIdx);
+                if (cell == null) {
+                    cell = row.createCell(colIdx);
+                }
+
+                CellStyle style = workbook.createCellStyle();
+
+                // 鑻ヨ鍗曞厓鏍煎凡鏈夋牱寮忥紝澶嶅埗
+                if (cell.getCellStyle() != null) {
+                    style.cloneStyleFrom(cell.getCellStyle());
+                }
+
+                // 璁剧疆宸﹀彸杈规
+                style.setBorderLeft(s.getBorderLeft());
+                style.setBorderRight(s.getBorderRight());
+                style.setBorderTop(s.getBorderTop());
+                style.setBorderBottom(s.getBorderBottom());
+                style.setAlignment(s.getAlignment());
+                style.setVerticalAlignment(VerticalAlignment.CENTER);
+                cell.setCellStyle(style);
+            }
+        }
+    }
     @SneakyThrows
-    public void createPrintOrderTemplate(ExportProductionPrintOrderDto printOrderDto, InputStream templateInputStream) {
+    public byte[] createPrintOrderTemplate(ExportProductionPrintOrderDto printOrderDto, InputStream templateInputStream) {
 
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 
@@ -62,11 +114,20 @@
         } catch (Exception e) {
             throw new ServiceException("excel 鐢熸垚澶辫触! " + e.getMessage());
         }
-
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
         try {
             Workbook workbook = WorkbookFactory.create(
                     new ByteArrayInputStream(byteArrayOutputStream.toByteArray())
             );
+
+            CellStyle cellStyleTblr = workbook.createCellStyle();
+            cellStyleTblr.setBorderBottom(BorderStyle.THIN);
+            cellStyleTblr.setBorderTop(BorderStyle.THIN);
+            cellStyleTblr.setBorderLeft(BorderStyle.THIN);
+            cellStyleTblr.setBorderRight(BorderStyle.THIN);
+            cellStyleTblr.setWrapText(true);
+            cellStyleTblr.setAlignment(HorizontalAlignment.CENTER);
+            cellStyleTblr.setVerticalAlignment(VerticalAlignment.CENTER);
 
             Sheet sheet = workbook.getSheetAt(0);
 
@@ -80,25 +141,53 @@
             ClientAnchor anchor = helper.createClientAnchor();
             anchor.setCol1(6);  // 绗�7鍒楋紙G鍒楋級
             anchor.setRow1(7);
-            anchor.setDx1(1082700); // 鍚戝彸绉诲姩锛堢害鍗婃牸锛�
+            anchor.setDx1(142700); // 鍚戝彸绉诲姩锛堢害鍗婃牸锛�
+            anchor.setDy1(222700);
 
-
+            anchor.setCol2(8);  // 瀹藉害缁堢偣
+            anchor.setRow2(9);  // 楂樺害缁堢偣
 
             Picture picture = drawing.createPicture(anchor, pictureIdx);
-            picture.resize();
+//            picture.resize(0.5);
 
-            ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+
+            // 杩涜鍚堝苟鎿嶄綔 鍒囨枡绀烘剰鍥鹃儴鍒�
+            int startRow = 7;
+            int size = printOrderDto.getMaterialInfo().size();
+            int qlEndRow = startRow + size - 1 + 4;
+            mergeWithBorder(sheet, workbook, new CellRangeAddress(
+                    startRow,
+                    qlEndRow,
+                    6,
+                    7
+            ), cellStyleTblr);
+
+            // 浜у搧澶囨敞
+            mergeWithBorder(sheet, workbook, new CellRangeAddress(
+                    6+1+printOrderDto.getMaterialInfo().size() +0,
+                    6+1+printOrderDto.getMaterialInfo().size() +1,
+                    0,
+                    5
+            ), cellStyleTblr);
+
+            int zbRowStart = qlEndRow + 2 + CollUtil.size(printOrderDto.getPlateMaking()) +1;
+            int zbRowEnd = zbRowStart + CollUtil.size(printOrderDto.getProcessContent());
+            mergeWithBorder(sheet, workbook, new CellRangeAddress(
+                    zbRowStart,
+                    zbRowEnd,
+                    4,
+                    7
+            ), cellStyleTblr);
+
+
             workbook.write(out);
             workbook.close();
-
-            FileUtil.writeBytes(
-                    out.toByteArray(),
-                    "/Users/ONEX/Downloads/uploads/" + IdUtil.simpleUUID() + ".xlsx"
-            );
-
         } catch (Exception e) {
             throw new ServiceException("鍥剧墖澶勭悊澶辫触! " + e.getMessage());
         }
+
+        return out.toByteArray();
     }
 
 }

--
Gitblit v1.9.3