【问题标题】:Pulling Data from Word Form从 Word 表单中提取数据
【发布时间】:2015-04-20 20:09:09
【问题描述】:

使用 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;
    }

所以问题是:如何从文档中的控件中提取数据,以及如何使用 NetOffice 或其他包?

【问题讨论】:

  • 这里是一个可能有用的起点:(stackoverflow.com/questions/9518275/…)。 “文档设置”是指边距、字体、行距等属性吗?这些很容易得到。顺便说一句,根据 NetOffice 文档,api 方法与 Office Interop 方法完全相同,这应该有助于您搜索一下。

标签: c# ms-office text-extraction


【解决方案1】:

我没有费心去实现 NetOffice,但是命令应该大部分是相同的(可能除了实现和处理方法)。

        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
        string file = "C:\\Hello World.docx";
        Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(file);

        // look for a specific type of Field (there are about 200 to choose from).
        foreach (Field f in doc.Fields)
        {
            if (f.Type == WdFieldType.wdFieldDate)
            {
                //do something
            }
        }

        // example of the myriad properties that could be associated with "document settings"
        WdProtectionType protType = doc.ProtectionType;
        if (protType.Equals(WdProtectionType.wdAllowOnlyComments))
        {
            //do something else
        }

MSDN reference on Word Interop 是您可以在 Word 文档中找到有关您需要访问的任何内容的信息的地方。

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

        string author = doc.BuiltInDocumentProperties("Author").Value;
        string name = doc.Name; // this gives you the file name.
                 //  not clear what you mean by "title"

就试图了解您从“旧版控件”中获得的文本而言,我需要更多信息来确切了解您从哪种控件中提取。尝试从文档本身中获取控件/文本框/表单/等的名称,然后在 Google 上查找该属性。

作为一个在黑暗中的刺,这是一个从文档中的文本框获取文本的(不完整的)示例:

        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<string> contentControlText = new List<string>();
        foreach(ContentControl CC in doc.ContentControls)
        {
            if (CC.Type == WdContentControlType.wdContentControlRichText)
            {
                contentControlText.Add(CC.Range.Text);
            }
        }

【讨论】:

  • 谢谢,我只是切换到互操作
  • 我认为字段的定义存在重叠......我的意思是外行意义上的字段。将其更改为控件。这实际上很难转录,因为字段、控件和表单已经是系统的一部分。所以文档中有问题提示用户单击一个框并输入一个响应。我需要使用代码从文档中提取对问题的响应。响应似乎是所谓的“遗留控件”。此外,我需要从面向作者、标题和其他数据的文档中提取设置。
猜你喜欢
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 2013-11-10
  • 2011-04-03
  • 1970-01-01
  • 2011-10-08
  • 2013-12-01
  • 2021-05-26
相关资源
最近更新 更多