1

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

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

那么解决方案是什么?

4

1 回答 1

3

在网上搜索和对文档稍作嗅探后,我发现 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);

        }
    }
}
于 2012-11-14T11:15:01.013 回答