From 5de5db24efe1b25896c37f395124a207584bcb38 Mon Sep 17 00:00:00 2001
From: fei <791364011@qq.com>
Date: 星期四, 01 一月 2026 21:25:54 +0800
Subject: [PATCH] 修改了对应代码

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java |  212 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 178 insertions(+), 34 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java
index 62fdb18..4631cea 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java
@@ -14,12 +14,10 @@
 import com.ruoyi.common.utils.file.ImageUtils;
 import com.ruoyi.common.utils.reflect.ReflectUtils;
 import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
+import org.apache.poi.hssf.usermodel.*;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellRangeAddressList;
-import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
-import org.apache.poi.hssf.usermodel.HSSFDataValidation;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
 import org.apache.poi.hssf.usermodel.HSSFDataValidation;
@@ -61,6 +59,14 @@
      * 瀵煎嚭绫诲瀷锛圗XPORT:瀵煎嚭鏁版嵁锛汭MPORT锛氬鍏ユā鏉匡級
      */
     private Excel.Type type;
+
+    public Workbook getWb() {
+        return wb;
+    }
+
+    public void setWb(Workbook wb) {
+        this.wb = wb;
+    }
 
     /**
      * 宸ヤ綔钖勫璞�
@@ -292,10 +298,34 @@
                 if(index == 1) {
                     // 鍚堝苟绗竴琛岀殑鍓�5涓崟鍏冩牸
                     sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
+                    // 鑾峰彇椤甸潰璁剧疆
+                    // 蹇呴』鍏堣缃负true
+                    // 椤甸潰甯冨眬璁剧疆
+                    sheet.setAutobreaks(true);
+                    sheet.setFitToPage(true);
 
-                    // 鍒涘缓琛屽苟璁剧疆楂樺害
+                    // 鑾峰彇鎵撳嵃璁剧疆
+                    PrintSetup printSetup = sheet.getPrintSetup();
+
+                    // 璁剧疆涓�1椤靛搴�
+                    printSetup.setFitWidth((short) 1);   // 瀹藉害璋冩暣涓�1椤�
+                    printSetup.setFitHeight((short) 0);  // 楂樺害涓嶉檺鍒�
+
+                    // 鍏朵粬鎵撳嵃璁剧疆
+                    printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE);  // A4绾�
+                    printSetup.setLandscape(false);  // 绾靛悜鎵撳嵃
+
+                    // 璁剧疆椤佃竟璺�
+                    sheet.setMargin(Sheet.LeftMargin, 0.5);
+                    sheet.setMargin(Sheet.RightMargin, 0.5);
+                    // 璁剧疆鎵撳嵃鍖哄煙锛屼粠绗�0琛屽埌绗�1000琛岋紝绗�0鍒楀埌绗�20鍒楋紙鏍规嵁瀹為檯鎯呭喌璋冩暣锛�
+                 //   sheet.setPrintArea(0, 0, 20, 0, 1000);
+                    // 濡傛灉鏄痠ndex涓�1鐨剆heet锛岃缃《绔爣棰樿
+
+                        // 灏嗙1琛岋紙绱㈠紩涓�0锛夎缃负姣忛〉閲嶅鐨勬爣棰樿
+                        sheet.setRepeatingRows(CellRangeAddress.valueOf("$1:$4"));
                      row = sheet.createRow(0);
-                    row.setHeight((short)(40 * 40));
+                    row.setHeight((short) (37*20));
 
                     // 鍒涘缓鍗曞厓鏍煎苟娣诲姞鍥剧墖
                     Cell cell = row.createCell(0);
@@ -304,16 +334,17 @@
                       //  if (Files.exists(Paths.get(imagePath))) {
                           //  byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
                             // 灏哹yte鏁扮粍杞崲鎴恑nputstream锛屼互渚挎彃鍏ュ埌Excel涓�
-                            ClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 5),
+                            ClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 100, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 5),
                                     cell.getRow().getRowNum() + 1);
+                            // 璁剧疆閿氱偣绫诲瀷涓篗OVE_AND_RESIZE锛屼娇鍥剧墖鍙互闅忓崟鍏冩牸璋冩暣澶у皬
+                            anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
                                     // 璁$畻灞呬腑浣嶇疆
-                    int col1 = 1; // 涓棿鍒�
-                    int col2 = col1 + 5;
+                    int col1 = 3; // 涓棿鍒�
+                    int col2 = col1 + 2;
                     anchor.setCol1(col1);
                     anchor.setCol2(col2);
                     anchor.setRow1(0);
                     anchor.setRow2(1);
-                  //          anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
 //                            anchor.set
 //                            anchor.setHorizontallyCenter(true);
 
@@ -333,27 +364,54 @@
                    //     log.error("娣诲姞鍥剧墖澶辫触: {}", e.getMessage());
                    // }
                     row = sheet.createRow(1);
+                    row.setHeight((short) (32.5*20));
                     sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));
                     Cell titleCell = row.createCell(0);
-                    titleCell.setCellValue("鍗峰唴鐩綍");
-                    // 璁剧疆鍔犵矖鍜屽眳涓牱寮�
+                    titleCell.setCellValue("鍗�      鍐�      鐩�      褰�");
+                    // 璁剧疆鍔犵矖銆佸畫浣撱��18鍙峰瓧浣撳拰灞呬腑鏍峰紡
                     CellStyle style = wb.createCellStyle();
                     Font font = wb.createFont();
                     font.setBold(true);
                     font.setFontName("瀹嬩綋");
+                    font.setFontHeightInPoints((short) 18);
                     style.setFont(font);
                     style.setAlignment(HorizontalAlignment.CENTER);
                     titleCell.setCellStyle(style);
                     row = sheet.createRow(2);
+                    row.setHeight((short) (20*20));
                //
-                    row.createCell(4).setCellValue("妗e彿:");
+                    Cell dnumCell = row.createCell(4);
+                    dnumCell.setCellValue("妗e彿:");
+                    // 璁剧疆鍔犵矖銆佸畫浣撱��18鍙峰瓧浣撳拰灞呬腑鏍峰紡
+                    CellStyle style1 = wb.createCellStyle();
+                    Font font1 = wb.createFont();
+                    font1.setBold(true);
+                    font1.setFontName("瀹嬩綋");
+                    font1.setFontHeightInPoints((short) 12);
+                    style1.setFont(font1);
+                    style1.setAlignment(HorizontalAlignment.RIGHT);
+                    dnumCell.setCellStyle(style1);
                  //   row.createCell(4).setCellValue("妗e彿:");
                     //鎷垮埌妗e彿
                     String recordId = list.get(index).getRecordId();
-                    row.createCell(5).setCellValue(recordId);
+                    // 鍒涘缓甯︽湁涓嬪垝绾挎牱寮忕殑鍗曞厓鏍�
+                    Cell recordIdCell = row.createCell(5);
+                    recordIdCell.setCellValue(recordId);
+                    // 鍒涘缓涓嬪垝绾挎牱寮�
+                    CellStyle underlineStyle = wb.createCellStyle();
+                    Font underlineFont = wb.createFont();
+                    underlineFont.setFontName("瀹嬩綋");
+                    underlineFont.setFontHeightInPoints((short) 11);
+                    underlineFont.setUnderline(Font.U_SINGLE); // 璁剧疆涓嬪垝绾�
+                    underlineStyle.setFont(underlineFont);
+                    recordIdCell.setCellStyle(underlineStyle);
                     row = sheet.createRow(3);
+                    row.setHeight((short) (17.5*20));
+                       row = sheet.createRow(4);
+//                     // 娣诲姞鎬婚〉鏁板拰褰撳墠椤电爜淇℃伅
+//                     row.createCell(5).setCellValue("鎬婚〉鏁�: " + list.size() + ", 褰撳墠椤�: " + (index + 1));
+//   row = sheet.createRow(4);
                     column = 0;
-
                 } else {
                     // 鏅�歴heet澶勭悊
                  //   recordId = (DocumentMaterialsVo)list.get(0)
@@ -464,7 +522,7 @@
 //        int endNo = Math.min(startNo + sheetSize, list.size());
         for (int i = 0; i < list.size(); i++) {
             if(index==1)
-                row = sheet.createRow(i + 4 );
+                row = sheet.createRow(i + 5 );
             else
                 row = sheet.createRow(i + 1 );
             // 寰楀埌瀵煎嚭瀵硅薄.
@@ -504,32 +562,46 @@
         style.setWrapText(true);
         Font dataFont = wb.createFont();
         dataFont.setFontName("Arial");
-        dataFont.setFontHeightInPoints((short) 10);
+        dataFont.setFontHeightInPoints((short) 12);
         style.setFont(dataFont);
         styles.put("data", style);
 
-        style = wb.createCellStyle();
-        style.cloneStyleFrom(styles.get("data"));
-        style.setAlignment(HorizontalAlignment.CENTER);
-        style.setVerticalAlignment(VerticalAlignment.CENTER);
-        style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
-        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
-        Font headerFont = wb.createFont();
-        headerFont.setFontName("Arial");
-        headerFont.setFontHeightInPoints((short) 10);
-        headerFont.setBold(true);
-        headerFont.setColor(IndexedColors.WHITE.getIndex());
-        style.setFont(headerFont);
-        // 琛ㄥご涔熻缃嚜鍔ㄦ崲琛�
-        style.setWrapText(true);
-        styles.put("header", style);
+        // 鍒涘缓琛ㄥご鏍峰紡鏄犲皠锛屾牴鎹瓻xcel娉ㄨВ鍔ㄦ�佺敓鎴�
+        Map<String, CellStyle> headerStyles = new HashMap<String, CellStyle>();
+        for (Object[] os : fields)
+        {
+            Excel excel = (Excel) os[1];
+            String key = StringUtils.format("header_{}_{}_{}_{}", excel.headerColor(), excel.headerBackgroundColor(), excel.headerFontName(), excel.headerFontBold());
+            if (!headerStyles.containsKey(key))
+            {
+                 style = wb.createCellStyle();
+                style.cloneStyleFrom(styles.get("data"));
+                style.setAlignment(HorizontalAlignment.CENTER);
+                style.setVerticalAlignment(VerticalAlignment.CENTER);
+                // 鏍规嵁娉ㄨВ璁剧疆琛ㄥご鑳屾櫙鑹� - 宸叉敞閲婏紝琛ㄥご涓嶆樉绀鸿儗鏅壊
+                /*if (excel.headerBackgroundColor() != IndexedColors.WHITE) {
+                    style.setFillForegroundColor(excel.headerBackgroundColor().index);
+                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+                }*/
+                // 璁剧疆琛ㄥご鑷姩鎹㈣
+                style.setWrapText(true);
+                Font headerFont = wb.createFont();
+                headerFont.setFontName(excel.headerFontName());
+                headerFont.setFontHeightInPoints((short) 12);
+                headerFont.setBold(excel.headerFontBold());
+                headerFont.setColor(excel.headerColor().index);
+                style.setFont(headerFont);
+                headerStyles.put(key, style);
+            }
+        }
+        styles.putAll(headerStyles);
 
         style = wb.createCellStyle();
         style.setAlignment(HorizontalAlignment.CENTER);
         style.setVerticalAlignment(VerticalAlignment.CENTER);
         Font totalFont = wb.createFont();
         totalFont.setFontName("Arial");
-        totalFont.setFontHeightInPoints((short) 10);
+        totalFont.setFontHeightInPoints((short) 12);
         style.setFont(totalFont);
         // 鎬昏琛屼篃璁剧疆鑷姩鎹㈣
         style.setWrapText(true);
@@ -568,7 +640,9 @@
         // 鍐欏叆鍒椾俊鎭�
         cell.setCellValue(attr.name());
         setDataValidation(attr, row, column);
-        cell.setCellStyle(styles.get("header"));
+        // 鏍规嵁Excel娉ㄨВ鍔ㄦ�侀�夋嫨琛ㄥご鏍峰紡
+        String key = StringUtils.format("header_{}_{}_{}_{}", attr.headerColor(), attr.headerBackgroundColor(), attr.headerFontName(), attr.headerFontBold());
+        cell.setCellStyle(styles.get(key));
         return cell;
     }
 
@@ -947,6 +1021,42 @@
         } else {
             wb.setSheetName(index, sheetName + index);
         }
+        // 璁剧疆椤甸潰灞炴��
+        sheet.setFitToPage(true);
+        PrintSetup printSetup = sheet.getPrintSetup();
+        printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE); // 璁剧疆涓篈4绾稿紶
+        printSetup.setLandscape(false); // 绾靛悜鎵撳嵃
+        printSetup.setFitWidth((short) 1);  // 瀹藉害璋冩暣涓�1椤�
+        printSetup.setFitHeight((short) 0); // 0琛ㄧず楂樺害涓嶉檺鍒堕〉鏁帮紙鑷姩锛�
+        // 璁剧疆椤佃竟璺�
+        sheet.setMargin(Sheet.TopMargin, 1.0); // 澧炲姞椤堕儴杈硅窛浠ュ绾抽〉鐪�
+        sheet.setMargin(Sheet.BottomMargin, 0.5);
+        sheet.setMargin(Sheet.LeftMargin, 0.5);
+        sheet.setMargin(Sheet.RightMargin, 0.5);
+        sheet.setMargin(Sheet.HeaderMargin, 0.5); // 璁剧疆椤电湁杈硅窛
+        sheet.setMargin(Sheet.FooterMargin, 0.5); // 璁剧疆椤佃剼杈硅窛
+        // 璁剧疆鎵撳嵃鍖哄煙锛屼粠绗�0琛屽埌绗�1000琛岋紝绗�0鍒楀埌绗�20鍒楋紙鏍规嵁瀹為檯鎯呭喌璋冩暣锛�
+       //sheet.setPrintArea(0, 0, 20, 0, 1000);
+        // 璁剧疆椤电湁锛屾樉绀烘�婚〉鏁板拰褰撳墠椤电爜
+        if (wb instanceof HSSFWorkbook) {
+            HSSFWorkbook hssfWb = (HSSFWorkbook) wb;
+            HSSFSheet hssfSheet = hssfWb.getSheetAt(index);
+            HSSFHeader header = hssfSheet.getHeader();
+            // 璁剧疆椤电湁瀛椾綋
+         //   header.setFontName("瀹嬩綋");
+         //   header.setFontSize((short) 10);
+            // 璁剧疆椤电湁鍐呭
+            hssfSheet.setMargin(Sheet.HeaderMargin, 0.5); 
+            header.setRight("鎬婚〉鏁�: &P, 褰撳墠椤�: &N");
+            // 璁剧疆椤电湁宸﹀榻愬拰鍙冲榻愬唴瀹癸紙鍙�夛級
+            header.setLeft("鏂囨。鏍囬");
+            header.setRight("&D &T"); // 鏃ユ湡鍜屾椂闂�
+        }
+        // 濡傛灉鏄痠ndex涓�1鐨剆heet锛岃缃《绔爣棰樿
+        if (index == 1) {
+            // 灏嗙1琛岋紙绱㈠紩涓�0锛夎缃负姣忛〉閲嶅鐨勬爣棰樿
+            sheet.setRepeatingRows(CellRangeAddress.valueOf("$1:$4"));
+        }
     }
     /**
      * 鍒涘缓宸ヤ綔琛�
@@ -957,6 +1067,40 @@
         this.sheet = wb.createSheet();
         this.styles = createStyles(wb);
         wb.setSheetName(index, sheetName);
+        // 鑾峰彇椤甸潰璁剧疆
+        // 蹇呴』鍏堣缃负true
+        sheet.setFitToPage(true);
+        PrintSetup printSetup = sheet.getPrintSetup();
+        printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE); // 璁剧疆涓篈4绾稿紶
+        printSetup.setLandscape(false); // 绾靛悜鎵撳嵃
+        printSetup.setFitWidth((short) 1);  // 瀹藉害璋冩暣涓�1椤�
+        printSetup.setFitHeight((short) 0); // 0琛ㄧず楂樺害涓嶉檺鍒堕〉鏁帮紙鑷姩锛�
+        // 璁剧疆椤佃竟璺�
+        sheet.setMargin(Sheet.TopMargin, 1.0); // 澧炲姞椤堕儴杈硅窛浠ュ绾抽〉鐪�
+        sheet.setMargin(Sheet.BottomMargin, 0.5);
+        sheet.setMargin(Sheet.LeftMargin, 0.5);
+        sheet.setMargin(Sheet.RightMargin, 0.5);
+        sheet.setMargin(Sheet.HeaderMargin, 0.5); // 璁剧疆椤电湁杈硅窛
+        sheet.setMargin(Sheet.FooterMargin, 0.5); // 璁剧疆椤佃剼杈硅窛
+        // 璁剧疆鎵撳嵃鍖哄煙锛屼粠绗�0琛屽埌绗�1000琛岋紝绗�0鍒楀埌绗�20鍒楋紙鏍规嵁瀹為檯鎯呭喌璋冩暣锛�
+      //  sheet.setPrintArea(0, 0, 20, 0, 1000);
+        // 璁剧疆椤电湁锛屾樉绀烘�婚〉鏁板拰褰撳墠椤电爜
+        if (wb instanceof HSSFWorkbook) {
+            HSSFWorkbook hssfWb = (HSSFWorkbook) wb;
+            HSSFSheet hssfSheet = hssfWb.getSheetAt(index);
+            HSSFHeader header = hssfSheet.getHeader();
+            // 璁剧疆椤电湁瀛椾綋
+           // header.setFontName("瀹嬩綋");
+           // header.setFontSize((short) 10);
+            // 璁剧疆椤电湁鍐呭
+            hssfSheet.setMargin(Sheet.HeaderMargin, 2.1);
+            header.setRight("鍏� &N 椤�   绗� &P椤�");
+        }
+        // 濡傛灉鏄痠ndex涓�1鐨剆heet锛岃缃《绔爣棰樿
+        if (index == 1) {
+            // 灏嗙1琛岋紙绱㈠紩涓�0锛夎缃负姣忛〉閲嶅鐨勬爣棰樿
+            sheet.setRepeatingRows(CellRangeAddress.valueOf("$1:$3"));
+        }
     }
     /**
      * 鑾峰彇鍗曞厓鏍煎��
@@ -998,4 +1142,4 @@
         }
         return val;
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.1