|
@@ -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;
|
|
|
|
|
|
}
|
|
|
|