如何使用openxml阅读word文档 [英] How to read word document using openxml

查看:118
本文介绍了如何使用openxml阅读word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我想使用openxml从word文档中读取一些特定内容。我已经尝试过以下代码来首先阅读文档。但我得到文件包含损坏的数据。

是否有人遇到过openxml word文档要求。请让我有一个示例代码。在此先感谢。



我尝试过:



Actually i want to read some particular content from word document using openxml. I've tried the below code for reading the document first. but i'm getting "file contains corrupted data".
Is anyone came across openxml word document requirement. Kindly let me have a sample code. Thanks in advance.

What I have tried:

 public void main..
    {
        
            string path = "";
            path = path + "D:\\OpenXml_Word\\Test_Docx.doc";
            SearchWordIsMatched(path);
        
    }


public void SearchWordIsMatched(string path)
    {
        try
        {
            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(path, true))
            {
                Stream stream = File.Open(path, FileMode.Open);
                Body body = wordDoc.MainDocumentPart.Document.Body;
                string content = body.InnerText;

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

推荐答案

你好,



从Microsoft查看本教程:

> 如何:打开文字处理文档以进行只读访问(Open XML SDK) [ ^ ]



@JAFC
Hello,

Check this tutorial from Microsoft:
> How to: Open a word processing document for read-only access (Open XML SDK)[^]

@JAFC


这篇关于如何使用openxml阅读word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆