首先引用服务后
导入using word = Microsoft.Office.Interop.Word;
word.Application app = new Microsoft.Office.Interop.Word.Application();
object unknow = Type.Missing;
try
{
word.Document doc = null;
app.Visible = false;//文本的展示是否可见
string str = @"D:\1.docx";
object file = str;
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);
string tempss = doc.Sentences[2].Text;//打开Word的段数
Console.WriteLine(tempss);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
app.Quit(ref unknow,ref unknow,ref unknow);
}
GC.Collect();//机制回收