1.添加程序集引用:WindowsBase
2.添加nuget:DocumentFormat.OpenXml
3.代码:
var wordPath = @"C:\xxx.docx";
using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPath, true))
{
Body body = doc.MainDocumentPart.Document.Body;
foreach (var paragraph in body.Elements<Paragraph>())
{
Console.WriteLine(paragraph.InnerText);
txt.AppendLine(paragraph.InnerText);
}
}
4.word后缀不是:docx,会报错