如何通过OpenXml实现读取Word文档内容的示例?
- 内容介绍
- 文章标签
- 相关推荐
本文共计212个文字,预计阅读时间需要1分钟。
使用OpenXml读取Word内容注意事项:
1.确保Word后缀为.docx,若为.doc,需先转换为.docx;
2.需引入相关DLL:WindowsBase.dll、DocumentFormat.OpenXml;
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(); }
控制台显示
以上这篇OpenXml读取word内容的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计212个文字,预计阅读时间需要1分钟。
使用OpenXml读取Word内容注意事项:
1.确保Word后缀为.docx,若为.doc,需先转换为.docx;
2.需引入相关DLL:WindowsBase.dll、DocumentFormat.OpenXml;
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(); }
控制台显示
以上这篇OpenXml读取word内容的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

