C# 从Word表单中提取数据

C# 从Word表单中提取数据,c#,ms-office,text-extraction,C#,Ms Office,Text Extraction,使用C#,我需要从word文档中提取数据。我在项目中安装了NetOffice for word。数据分为两部分 首先,我需要从文档设置中提取数据 其次,我需要提取文档中控件的内容。字段的内容包括复选框、日期和一些段落。输入方法是通过控件实现的,因此必须有某种方式通过api与控件交互,但我不知道如何做到这一点 现在,我有以下代码从文档中提取平面文本: private static string wordDocument2String(string file) { NetOf

使用C#,我需要从word文档中提取数据。我在项目中安装了NetOffice for word。数据分为两部分

首先,我需要从文档设置中提取数据

其次,我需要提取文档中控件的内容。字段的内容包括复选框、日期和一些段落。输入方法是通过控件实现的,因此必须有某种方式通过api与控件交互,但我不知道如何做到这一点

现在,我有以下代码从文档中提取平面文本:

private static string wordDocument2String(string file)
    {
        NetOffice.WordApi.Application wordApplication = new NetOffice.WordApi.Application();
        NetOffice.WordApi.Document newDocument = wordApplication.Documents.Open(file);
        string txt = newDocument.Content.Text;
        wordApplication.Quit();
        wordApplication.Dispose();
        return txt;
    }
        List<string> textBoxText = new List<string>();
        foreach (Microsoft.Office.Interop.Word.Shape s in doc.Shapes)
        {
            textBoxText.Add(s.TextFrame.TextRange.Text); //this could result in an error if there are shapes that don't contain text.
        }

因此,问题是:如何使用NetOffice或其他软件包从文档的控件中提取数据,以及如何提取文档设置(如word中的标题、作者等)?

我没有费心实现NetOffice,但命令应该基本相同(实施和处置方法可能除外)

在Word文档中,您可以找到有关您需要访问的任何内容的信息

更新: 阅读您的评论后,您可以访问以下几个文档设置:

        string author = doc.BuiltInDocumentProperties("Author").Value;
        string name = doc.Name; // this gives you the file name.
                 //  not clear what you mean by "title"
就试图理解您从“遗留控件”中获取的文本而言,我需要更多关于您从中提取的控件类型的信息。请尝试从文档本身中获取控件名/textbox/form/etc,然后在Google上查找该属性

下面是一个从文档中的文本框中获取文本的(不完整)示例:

private static string wordDocument2String(string file)
    {
        NetOffice.WordApi.Application wordApplication = new NetOffice.WordApi.Application();
        NetOffice.WordApi.Document newDocument = wordApplication.Documents.Open(file);
        string txt = newDocument.Content.Text;
        wordApplication.Quit();
        wordApplication.Dispose();
        return txt;
    }
        List<string> textBoxText = new List<string>();
        foreach (Microsoft.Office.Interop.Word.Shape s in doc.Shapes)
        {
            textBoxText.Add(s.TextFrame.TextRange.Text); //this could result in an error if there are shapes that don't contain text.
        }
List textBoxText=new List();
foreach(doc.Shapes中的Microsoft.Office.Interop.Word.Shapes)
{
textBoxText.Add(s.TextFrame.TextRange.Text);//如果存在不包含文本的形状,则可能会导致错误。
}
另一种可能是内容控件,有几种类型。它们通常用于收集用户输入

下面是一些捕获富文本内容控件的代码:

        List<string> contentControlText = new List<string>();
        foreach(ContentControl CC in doc.ContentControls)
        {
            if (CC.Type == WdContentControlType.wdContentControlRichText)
            {
                contentControlText.Add(CC.Range.Text);
            }
        }
List contentControlText=new List();
foreach(文档ContentControls中的ContentControl抄送)
{
if(CC.Type==WdContentControlType.wdContentControlRichText)
{
contentControlText.Add(CC.Range.Text);
}
}

这里有一个可能有用的起点:()。通过“文档设置”,你是说边距、字体、行距等属性吗?这些属性很容易获取。顺便说一句,根据NetOffice文档,api方法与Office Interop方法完全相同,这应该有助于你的谷歌搜索。谢谢,我将切换到interopI。我认为字段的定义有重叠…我是指层中的字段nse。将其更改为控件。这实际上很难转录,因为字段、控件和表单已经是系统的一部分。因此,文档中存在问题,提示用户单击框并输入响应。我需要使用代码从文档中提取对问题的响应。响应似乎是s还有,我需要从文档中提取设置,其中作者、标题和其他数据是面向文档的。