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:UsersuserDesktop新建文件夹 (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内容的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- C#采用OpenXml给Word文档添加表格
- OpenXml读写Excel实例代码
- C#利用Openxml读取Excel数据实例