当前位置:主页 > 软件编程 > .NET代码 >

OpenXml读取word内容的实例

时间:2021-04-01 08:19:43 | 栏目:.NET代码 | 点击:

OpenXml读取word内容注意事项

1、使用OpenXml读取word内容,word后缀必须是".docx";如果word后缀是".doc"需要转成".docx"后,才可以读取;

2、需要引入相关dll;"WindowsBase.dll"、“DocumentFormat.OpenXml.dll”;

3、word大小>0字节(word大小为0字节会报错);

word内容

相关代码

 

static void Main(string[] args)
  {
   string wordPathStr = @"C:\Users\user\Desktop\新建文件夹 (2)\5.docx";
   using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
   {
    Body body = doc.MainDocumentPart.Document.Body;
    foreach (var paragraph in body.Elements<Paragraph>())
    {
     Console.WriteLine(paragraph.InnerText);
    }
   }
   Console.ReadLine();
  }

控制台显示

您可能感兴趣的文章:

相关文章