【问题标题】:OpenXML SDK getting ActiveX controls valueOpenXML SDK 获取 ActiveX 控件值
【发布时间】:2012-11-14 11:15:01
【问题描述】:

对于我在大学的一个项目,我必须创建一个 Word 文档形式的测试,并在其中添加一些 ActiveX 表单以供其他人完成。在此之后,我必须以编程方式从中提取答案并为测试打分。

我使用 OpenXML SDK 来处理文档,但它让我很头疼,因为我找不到获取 ActiveX 值的方法。

那么解决办法是什么?

【问题讨论】:

    标签: c# .net ms-word activex openxml-sdk


    【解决方案1】:

    在网上搜索了一下文档,我发现ActiveX控件数据可以在控件ID指定的文档部分找到。确定控件的类型有点棘手,因为我没有找到任何关于此的文档。显然,您必须从控件中获取“classid”属性并尝试将其与您知道的 classid 匹配。下面是确定三种类型控件值的代码。其余的 id 被标记为未知,您可以直观地将它们与您在文档中添加的那些进行匹配。

    
    using System;
    using System.Collections.Generic;
    using System.Xml.Linq;
    using System.Xml;
    using System.IO;
    using System.Text;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml.Packaging;
    
    namespace OpenXMLTest
    {
        class Program
        {
            const string textBoxId = "{8BD21D10-EC42-11CE-9E0D-00AA006002F3}";
            const string radioButtonId = "{8BD21D50-EC42-11CE-9E0D-00AA006002F3}";
            const string checkBoxId = "{8BD21D40-EC42-11CE-9E0D-00AA006002F3}";
    
            static void Main(string[] args)
            {
                string fileName = @"C:\Users\Andy\Desktop\test_l1demo.docx";
                using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, false))
                {
                    foreach (Control control in doc.MainDocumentPart.Document.Body.Descendants())
                    {
                        Console.WriteLine();
                        Console.WriteLine("Control {0}:", control.Name);
                        Console.WriteLine("Id: {0}", control.Id);
    
                        displayControlDetails(doc, control.Id);
                    }
                }
    
                Console.Read();
            }
    
            private static void displayControlDetails(WordprocessingDocument doc, StringValue controlId)
            {
                string classId, type, value;
    
                OpenXmlPart part = doc.MainDocumentPart.GetPartById(controlId);
                OpenXmlReader reader = OpenXmlReader.Create(part.GetStream());
                reader.Read();
                OpenXmlElement controlDetails = reader.LoadCurrentElement();
    
                classId = controlDetails.GetAttribute("classid", controlDetails.NamespaceUri).Value;
    
                switch (classId)
                {
                    case textBoxId:
                        type = "TextBox";
                        break;
                    case radioButtonId:
                        type = "Radio Button";
                        break;
                    case checkBoxId:
                        type = "CheckBox";
                        break;
                    default:
                        type = "Not known";
                        break;
                }
    
                value = "No value attribute"; //displays this if there is no "value" attribute found
                foreach (OpenXmlElement child in controlDetails.Elements())
                {
                    if (child.GetAttribute("name", controlDetails.NamespaceUri).Value == "Value")
                    {
                        //we've found the value typed by the user in this control
                        value = child.GetAttribute("value", controlDetails.NamespaceUri).Value;
                    }
                }
    
                reader.Close();
    
                Console.WriteLine("Class id: {0}", classId);
                Console.WriteLine("Control type: {0}", type);
                Console.WriteLine("Control value: {0}", value);
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      相关资源
      最近更新 更多