From 9b6fc58ae37b2745310e6942af1a4dae51a82c56 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 10 二月 2023 19:41:31 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/helper/WordHelper.java |   67 ++++++++++++++++++++-------------
 1 files changed, 40 insertions(+), 27 deletions(-)

diff --git a/src/main/java/com/lf/server/helper/WordHelper.java b/src/main/java/com/lf/server/helper/WordHelper.java
index fb365d6..311a543 100644
--- a/src/main/java/com/lf/server/helper/WordHelper.java
+++ b/src/main/java/com/lf/server/helper/WordHelper.java
@@ -20,9 +20,12 @@
      * 閫氳繃word妯℃澘鐢熸垚word鐨勪富鏂规硶
      */
     public static void generateWord(String inputFile, String outPutFile, Map<String, String> insertTextMap, List<String[]> addList) {
+        FileInputStream inputStream = null;
+        FileOutputStream outputStream = null;
+
         try {
-            FileInputStream inputStream = new FileInputStream(inputFile);
-            FileOutputStream outputStream = new FileOutputStream(outPutFile);
+            inputStream = new FileInputStream(inputFile);
+            outputStream = new FileOutputStream(outPutFile);
 
             // 鑾峰彇docx瑙f瀽瀵硅薄
             XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
@@ -37,11 +40,19 @@
 
             // 鍐欏叆鏁版嵁
             xwpfDocument.write(outputStream);
-
-            outputStream.close();
-            inputStream.close();
         } catch (Exception ex) {
             log.error(ex.getMessage(), ex);
+        } finally {
+            try {
+                if (outputStream != null) {
+                    outputStream.close();
+                }
+                if (inputStream != null) {
+                    inputStream.close();
+                }
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
+            }
         }
     }
 
@@ -107,23 +118,25 @@
      * 鎻掑叆鏁版嵁
      */
     private static void insertData(XWPFTable table, List<String[]> addList) {
-        XWPFTableRow oldRow = table.getRow(1);
-        for (int i = 1; i < addList.size(); i++) {
+        for (int i = 1, c = addList.size(); i < c; i++) {
             XWPFTableRow row = table.createRow();
-            row.setHeight(oldRow.getHeight());
         }
 
+        List<XWPFTableCell> oldCells = table.getRow(1).getTableCells();
         List<XWPFTableRow> rowList = table.getRows();
-        for (int i = 1; i < rowList.size(); i++) {
-            XWPFTableRow xwpfTableRow = rowList.get(i);
+        for (int i = 0, c = addList.size(); i < c; i++) {
+            XWPFTableRow xwpfTableRow = rowList.get(i + 1);
 
             List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells();
             for (int j = 0; j < tableCells.size(); j++) {
-                XWPFTableCell oldCell = oldRow.getTableCells().get(j);
+                XWPFTableCell oldCell = oldCells.get(j);
                 XWPFTableCell newCell = tableCells.get(j);
 
-                //newCell.setText(addList.get(i - 1)[j]);
-                setCellText(oldCell, newCell, addList.get(i - 1)[j]);
+                if (0 == i) {
+                    newCell.setText(addList.get(i)[j]);
+                } else {
+                    setCellText(oldCell, newCell, addList.get(i)[j]);
+                }
             }
         }
     }
@@ -197,13 +210,13 @@
             }
             if (tmpR.getCTR() != null) {
                 if (tmpR.getCTR().isSetRPr()) {
-                    CTRPr tmpRPr = tmpR.getCTR().getRPr();
-                    if (tmpRPr.isSetRFonts()) {
-                        CTFonts tmpFonts = tmpRPr.getRFonts();
-                        CTRPr cellRPr = cellR.getCTR().isSetRPr() ? cellR
+                    CTRPr tmpRpr = tmpR.getCTR().getRPr();
+                    if (tmpRpr.isSetRFonts()) {
+                        CTFonts tmpFonts = tmpRpr.getRFonts();
+                        CTRPr cellRpr = cellR.getCTR().isSetRPr() ? cellR
                                 .getCTR().getRPr() : cellR.getCTR().addNewRPr();
-                        CTFonts cellFonts = cellRPr.isSetRFonts() ? cellRPr
-                                .getRFonts() : cellRPr.addNewRFonts();
+                        CTFonts cellFonts = cellRpr.isSetRFonts() ? cellRpr
+                                .getRFonts() : cellRpr.addNewRFonts();
                         cellFonts.setAscii(tmpFonts.getAscii());
                         cellFonts.setAsciiTheme(tmpFonts.getAsciiTheme());
                         cellFonts.setCs(tmpFonts.getCs());
@@ -229,14 +242,14 @@
         cellP.setPageBreak(tmpP.isPageBreak());
         if (tmpP.getCTP() != null) {
             if (tmpP.getCTP().getPPr() != null) {
-                CTPPr tmpPPr = tmpP.getCTP().getPPr();
-                CTPPr cellPPr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr();
+                CTPPr tmpPpr = tmpP.getCTP().getPPr();
+                CTPPr cellPpr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr();
 
                 // 澶嶅埗娈佃惤闂磋窛淇℃伅
-                CTSpacing tmpSpacing = tmpPPr.getSpacing();
+                CTSpacing tmpSpacing = tmpPpr.getSpacing();
                 if (tmpSpacing != null) {
-                    CTSpacing cellSpacing = cellPPr.getSpacing() != null ? cellPPr
-                            .getSpacing() : cellPPr.addNewSpacing();
+                    CTSpacing cellSpacing = cellPpr.getSpacing() != null ? cellPpr
+                            .getSpacing() : cellPpr.addNewSpacing();
                     if (tmpSpacing.getAfter() != null) {
                         cellSpacing.setAfter(tmpSpacing.getAfter());
                     }
@@ -266,10 +279,10 @@
                 }
 
                 // 澶嶅埗娈佃惤缂╄繘淇℃伅
-                CTInd tmpInd = tmpPPr.getInd();
+                CTInd tmpInd = tmpPpr.getInd();
                 if (tmpInd != null) {
-                    CTInd cellInd = cellPPr.getInd() != null ? cellPPr.getInd()
-                            : cellPPr.addNewInd();
+                    CTInd cellInd = cellPpr.getInd() != null ? cellPpr.getInd()
+                            : cellPpr.addNewInd();
                     if (tmpInd.getFirstLine() != null) {
                         cellInd.setFirstLine(tmpInd.getFirstLine());
                     }

--
Gitblit v1.9.3