luocaiyang 11 月之前
父節點
當前提交
73ea39b17d
共有 3 個文件被更改,包括 43 次插入14 次删除
  1. 42 12
      wispro.sp.utility/CompareDocx.cs
  2. 0 1
      wispro.sp.winClient/Form1.Designer.cs
  3. 1 1
      wispro.sp.winClient/Form1.cs

+ 42 - 12
wispro.sp.utility/CompareDocx.cs

@@ -5,10 +5,10 @@ using System.Text;
 using DiffPlex;
 using System.IO.Packaging;
 using System.Text.RegularExpressions;
-using NPOI.XWPF.UserModel;
 using System.Linq;
 using Microsoft.Office.Interop.Word;
 
+
 namespace wispro.sp.utility
 {
     /// <summary>
@@ -96,7 +96,7 @@ namespace wispro.sp.utility
 
         private string _CompareResultString;
         /// <summary>
-        /// 修订版本的文字
+        /// 包括修订文字版本的文档
         /// </summary>
         public string CompareResultString
         {
@@ -191,19 +191,49 @@ namespace wispro.sp.utility
             
         }
 
-        private string GetDocTxt(string filepath)
+        private string GetDocTxt(string filePath)
         {
-            Application wordApp = new Application();
-            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);
-            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(fileInfo.FullName);
-            string text = doc.Content.Text;
+            Application word = null;
+            Document doc = null;
+            string content = string.Empty;
+
+            try
+            {
+                // 创建Word应用实例
+                word = new Application();
+                // 打开Word文档
+                doc = word.Documents.Open(filePath);
+                // 读取文档内容
+                content = doc.Content.Text;
+
+                List<string> lines = content.Split("\r").ToList();
+                return List2String(lines);
 
-            List<string> lines = text.Split("\r").ToList();
-            text = List2String(lines);
+            }
+            catch (Exception ex)
+            {
+                throw new Exception($"读取Word文档时发生错误: {ex.Message}");
+            }
+            finally
+            {
+                // 关闭文档
+                if (doc != null)
+                {
+                    doc.Close();
+                    #pragma warning disable CA1416 // 验证平台兼容性
+                    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
+                    #pragma warning restore CA1416 // 验证平台兼容性
+                }
+                // 退出Word应用
+                if (word != null)
+                {
+                    word.Quit();
+                    #pragma warning disable CA1416 // 验证平台兼容性
+                    System.Runtime.InteropServices.Marshal.ReleaseComObject(word);
+                    #pragma warning restore CA1416 // 验证平台兼容性
+                }
+            }
 
-            doc.Close();
-            wordApp.Quit();
-            return text;
 
         }
 

+ 0 - 1
wispro.sp.winClient/Form1.Designer.cs

@@ -126,7 +126,6 @@ namespace wispro.sp.winClient
             button9.TabIndex = 1;
             button9.Text = "获取一次OA授权数据";
             button9.UseVisualStyleBackColor = true;
-            button9.Visible = false;
             button9.Click += button9_Click;
             // 
             // Form1

+ 1 - 1
wispro.sp.winClient/Form1.cs

@@ -913,7 +913,7 @@ namespace wispro.sp.winClient
             return retStr;
         }
 
-        string strAPIBaseUri = "http://localhost:39476"; //"http://1.116.113.26:81";//    "http://47.106.221.167:8081"; //  
+        string strAPIBaseUri = "http://1.116.113.26:81";//"http://localhost:39476"; //    "http://47.106.221.167:8081"; //  
 
         userToken Token;