OpenXML SDK获得ActiveX控件的价值 [英] OpenXML SDK getting ActiveX controls value

查看:140
本文介绍了OpenXML SDK获得ActiveX控件的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For one of my projects in college I had to create a test as a Word document and add some ActiveX forms in it to be completed by another person. After this I had to programmatically extract the answers from it and put a grade on the test.

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

I used OpenXML SDK for processing the document, but it gave me headaches because I couldn't find a way to get the ActiveX values.

那解决方案是什么?

推荐答案

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

After searching the Internet and a bit of sniffing on the document I have found that the ActiveX control data can be found in a document part specified by the control ID. Determining the type of control is a bit tricky because I didn't find any documentation about this. Apparently you must get the "classid" attribute from a control and try to match it to the classids you know. Below is the code for determining the values for three types of controls. The rest of the ids are marked as not known and you can match them intuitively to the ones you added in the document.


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);

        }
    }
}

这篇关于OpenXML SDK获得ActiveX控件的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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