您的位置:首页 > 运维架构

OpenXml读取word内容注意事项

2017-12-12 18:42 260 查看

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();
}

控制台显示

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: