首先要添加引用com组件:
然后引用:
using Word = Microsoft.Office.Interop.Word;
/// <summary>
/// 读取 word文档 返回内容
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string GetWordContent(string path)
{
try
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Type wordType = app.GetType();
Microsoft.Office.Interop.Word.Document doc = null;
object unknow = Type.Missing;
app.Visible = false;
object file = path;
doc = app.Documents.Open(ref file,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow);
int count = doc.Paragraphs.Count;
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= count; i++)
{
sb.Append(doc.Paragraphs[i].Range.Text.Trim());
}
doc.Close(ref unknow, ref unknow, ref unknow);
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, app, null);
doc = null;
app = null;
//垃圾回收
GC.Collect();
GC.WaitForPendingFinalizers();
string temp = sb.ToString();
//if (temp.Length > 200)
// return temp.Substring(0, 200);
//else
return temp;
}
catch
{
return "";
}
}
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有