简体   繁体   中英

How to set Checkbox value in Excel Sheet using OpenXML

I've an excel template with ActiveX controls(check boxes, radio buttons etc) in the sheet.

I need to get the check box reference programmatically and change the value to checked or unchecked.

I couldn't find any proper lead after brief internet search. Can anyone please help me with this?

I'm using visual studio 2019, OpenXML 2.5 and C# with.Net Framework 4.7.

This.. turned out to be quite complicated.. but a nice challenge:)

It is possible but hella ugly. First: Credits. Those two questions here and here got me started, the rest is through trial and error. (Note: All code samples follow in succession, a complete sample is at the end, as it is quite long)

First step: Finding the CheckBox All ActiveX controls are stored as a Control, we need to find the control in question.

using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fileName, true))
        {
            foreach (var control in doc.WorkbookPart.WorksheetParts.First().Worksheet.Descendants<Control>())
            {
                Console.WriteLine();
                Console.WriteLine("Control {0}:", control.Name);
                Console.WriteLine("Id: {0}", control.Id);

You can then find the specific control you look for by filtering the name.

Bonus: Identifying the type Based on one of the linked questions, we can identify the type of control as follows:

                var part = doc.WorkbookPart.WorksheetParts.First().GetPartById(control.Id) as EmbeddedControlPersistencePart;
                var xreader = OpenXmlReader.Create(part.GetStream());
                xreader.Read();

                var xml = xreader.LoadCurrentElement();
                var classID = xml.GetAttribute("classid", xml.NamespaceUri).Value;

                if (classID == "{8BD21D40-EC42-11CE-9E0D-00AA006002F3}") // it is a checkbox

Second Step: Getting those Attributes All ActiveX controls store their attributes as a binary file. So you'll need to parse and edit this binary file to change the tick mark. So lets first find the binary data

var binary = part.EmbeddedControlPersistenceBinaryDataParts.First();

Easy. Now we need to get the binary stream and make it editable by pulling it into a memory stream

using (var source = binary.GetStream())
{
    using (var reader = new BinaryReader(source))
    {
         using (var stream = new MemoryStream(reader.ReadBytes((int)source.Length)))
         {

Neat. Now some stupid binary parsing. I found the layout of the binary data by trial and error using HxD and manipulating attributes one by one. I've added my analysis at the end of this question. As xlsx-Files are just zip-files, you can find the binary relatively easily yourself in the path "xl\activeX"

First, lets skip to the desired position where the checkmark is saved

                                // Skip random junk
                                stream.Seek(20, SeekOrigin.Begin);

                                // read additional data flags & how many additional bytes are added
                                var flags = (byte) stream.ReadByte();
                                var skip = 0;

                                if ((flags & 0x01) != 0) // Attribute Block
                                    skip++;
                                if ((flags & 0x02) != 0) // BackColor Block
                                    skip++;
                                if ((flags & 0x04) != 0) // ForeColor Block
                                    skip++;

                                // skip some bytes
                                stream.Seek(2, SeekOrigin.Current);
                                flags = (byte) stream.ReadByte();

                                if ((flags & 0x01) != 0) // Special Effect Block
                                    skip++;
                                if ((flags & 0x04) != 0) // PicturePosition Block
                                    skip++;

                                // skip some flags
                                stream.Seek(4, SeekOrigin.Current);

                                // skip attribute blocks & some data blocks
                                stream.Seek(skip * 4 + 6 * 4, SeekOrigin.Current);

Then parse the active checkmark, invert it and save it back into the stream

                                var check = (byte) stream.ReadByte();

                                // check = 0x30: False
                                // check = 0x31: True
                                // check = 0x32: Undefined
                                bool? boolCheck = (check == 0x31);
                                if (check == 0x32) boolCheck = null;

                                // invert checkmark
                                boolCheck = !boolCheck;

                                if (boolCheck == null)
                                    check = 0x32;
                                if (boolCheck == true)
                                    check = 0x31;
                                if (boolCheck == false)
                                    check = 0x30;

                                stream.Seek(-1, SeekOrigin.Current);
                                stream.WriteByte(check);

Now write the new stream back and save everything. Done.

                                // ignore all the other stuff

                                stream.Seek(0, SeekOrigin.Begin);
                                binary.FeedData(stream);
                                doc.Save();
                            }
                        }
                    }

                    break;
                }
            }
        }

        Console.Read();

Note: the break is in there because the checkbox was found twice and I was too lazy to figure out why.

Conclusio And this is how you invert the checkmark of an activeX CheckBox in a spreadsheet using OpenXML.

Addendum 1 Here is the full code I came up with. The sample xlsm has just one activeX checkbox placed in it:

    static void Main(string[] args)
    {
        string fileName = @"C:\Users\***\Desktop\Test.xlsm";
        using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fileName, true))
        {
            foreach (var control in doc.WorkbookPart.WorksheetParts.First().Worksheet.Descendants<Control>())
            {
                Console.WriteLine();
                Console.WriteLine("Control {0}:", control.Name);
                Console.WriteLine("Id: {0}", control.Id);

                var part = doc.WorkbookPart.WorksheetParts.First().GetPartById(control.Id) as EmbeddedControlPersistencePart;
                var xreader = OpenXmlReader.Create(part.GetStream());
                xreader.Read();

                var xml = xreader.LoadCurrentElement();
                var classID = xml.GetAttribute("classid", xml.NamespaceUri).Value;

                if (classID == "{8BD21D40-EC42-11CE-9E0D-00AA006002F3}") // it is a checkbox
                {
                    var binary = part.EmbeddedControlPersistenceBinaryDataParts.First();
                    using (var source = binary.GetStream())
                    {
                        using (var reader = new BinaryReader(source))
                        {
                            using (var stream = new MemoryStream(reader.ReadBytes((int)source.Length)))
                            {
                                // Skip random junk
                                stream.Seek(20, SeekOrigin.Begin);

                                // read additional data flags & how many additional bytes are added
                                var flags = (byte) stream.ReadByte();
                                var skip = 0;

                                if ((flags & 0x01) != 0) // Attribute Block
                                    skip++;
                                if ((flags & 0x02) != 0) // BackColor Block
                                    skip++;
                                if ((flags & 0x04) != 0) // ForeColor Block
                                    skip++;

                                // skip some bytes
                                stream.Seek(2, SeekOrigin.Current);
                                flags = (byte) stream.ReadByte();

                                if ((flags & 0x01) != 0) // Special Effect Block
                                    skip++;
                                if ((flags & 0x04) != 0) // PicturePosition Block
                                    skip++;

                                // skip some flags
                                stream.Seek(4, SeekOrigin.Current);

                                // skip attribute blocks & some data blocks
                                stream.Seek(skip * 4 + 6 * 4, SeekOrigin.Current);

                                var check = (byte) stream.ReadByte();

                                // check = 0x30: False
                                // check = 0x31: True
                                // check = 0x32: Undefined
                                bool? boolCheck = (check == 0x31);
                                if (check == 0x32) boolCheck = null;

                                // invert checkmark
                                boolCheck = !boolCheck;

                                if (boolCheck == null)
                                    check = 0x32;
                                if (boolCheck == true)
                                    check = 0x31;
                                if (boolCheck == false)
                                    check = 0x30;

                                stream.Seek(-1, SeekOrigin.Current);
                                stream.WriteByte(check);

                                // ignore all the other stuff

                                stream.Seek(0, SeekOrigin.Begin);
                                binary.FeedData(stream);
                                doc.Save();
                            }
                        }
                    }

                    break;
                }
            }
        }

        Console.Read();
    }

Addendum 2 For anyone interested, here is the analysis of the binary format so far. Some Blocks will only be written, if some Flag is set (IIF), and all strings are padded to a multiple of 4 Bytes:

ClassID (first half is reverse byte order in a block)
40 1D D2 8B-42 EC-CE 11-9E 0D-00 AA 00 60 02 F3

Random Junk
00 02 38 00

Attribute Flags
        TrippleState: C0=F, E0=T
40  01  C0  80  01  00  00  00

 Cursor Flag
 |     ForeColor Flag
 |     |BackColor Flag
 |     ||Attribute Block
 |??? ?|||      
[0100 0000] 01 ...

          Accelerator Flag
          |   SpecialEffect Flag
          |   | Picture Position Flag
        ??|? ?|?|
... C0 [1000 0000] ...

Attributes (IIF Attribute Flag)
1B  08  80  2C

      BackStyle
      | Enable
      | |   Alignment
      | |    |       WordWrap
      | |    |       |            AutoSize
 ???? |?|? ??|? ???? |??? ???? ???| ????
[0001 1011 0000 1000 1000 0000 0010 1100]

Background Color (IIF BackColor Flag)
04  00  00  80  (Reverse order)

Foreground Color (IIF ForeColor Flag)
04  00  00  80  (Reverse order)

Random Junk
    Mouse Cursor
40  00  00  00

Length: Value
01  00  00  80

Length: Caption
09  00  00  80

Picture Position (IIF Picture Position Flag)
00  00  02  00 = 0, Left Top
03  00  05  00 = 1, Left Center
06  00  08  00 = 2, Left Bot
02  00  00  00 = 3, Right Top
....

Special Effect (IIF Special Effect Flag)
00  00  00  00

Accelerator Key (IIF Accelerator Flag)
61  00  00  00

Length: Group
06  00  00  80

Width (unknown Units)
B7 09 00 00

Height (unknown Units)
E5 02 00 00

Value (Padded to multiple of 4 Bytes)
30 = False
31 = True
32 = Undefined
30  xx  xx  xx

Caption (Padded to multiple of 4 Bytes)

Group Name (Padded to multiple of 4 Bytes)

Random Junk
00 02 18 00 35 00 00 00

Length: Font
07  00  00  80

Random Junk (Variable length? not deciphered)
E1 00 00 00 00 02 00 00

Font Name (Padded to multiple of 4 Bytes)

I think it is fairly complete, I covered most of the Properties exposed by the ActiveX, skipping the Bitmaps & Icons. But maybe there is some more; I wasn't able to decipher the last piece of junk at the end; it has variable length but I didn't really bother because it was after the checkmark.

I don't know if there's an easier way to do this but here's one way. The checkbox is stored as a VML drawing in the spreadsheet. Here's an example, with no error checking of course, that shows how to toggle it on or off.

    XNamespace ExcelNs = "urn:schemas-microsoft-com:office:excel";
    XName CheckBoxName = ExcelNs + "ClientData";
    XName CheckBoxTypeAttributeName = "ObjectType";
    const string CheckBoxType = "Checkbox";

    using (var doc = SpreadsheetDocument.Open("Temp.xlsx", true))
    {
        var wb = doc.WorkbookPart;
        var ws = wb.WorksheetParts.First();
        var drawing = ws.VmlDrawingParts.First();

        using (var reader = new StreamReader(drawing.GetStream()))
        {
            string content = reader.ReadToEnd();

            XDocument d = XDocument.Parse(content);

            var checkBox = d.Descendants()
                                .Where(e => e.Name == CheckBoxName && 
                                        e.Attribute(CheckBoxTypeAttributeName)?.Value == CheckBoxType).FirstOrDefault();


            if (null != checkBox)
            {
                var checkBoxValue = checkBox.Descendants(ExcelNs + "Checked").FirstOrDefault();

                bool ischecked = null != checkBoxValue;

                Console.WriteLine($"Checkbox is checked: {ischecked}");

                if (ischecked)
                    checkBoxValue.Remove();
                else
                    checkBox.Add(new XElement(ExcelNs + "Checked", "1"));

                reader.Close();

                using (var writer = new StreamWriter(drawing.GetStream()))
                {
                    d.Save(writer);
                }

                doc.Save();
            }
        }
    }

This example had a workbook with one sheet and one checkbox.

For anybody following this, CShark's answer was brilliant, and formed the basis for my solution. For my needs, it allows me to open a Word DOCX in a memory stream (using Open XML SDK), copy the contents into a new Word DOCX as a stream, and then open any ActiveX control buttons (radio buttons) into the same stream. We can then modify the bytes in the stream that relate to the checked/unchecked state of the radio, and save this back to the stream. We are effectively modifying the properties of the ActiveX radio button using the Open XML SDK and a bit of jiggery-pokery with the underlying bytes.

This is an outline of our solution. Note, you will need to install the 'OffVis' tool from Microsoft to inspect the contents of each ActiveX control BIN file. (To find the BIN files, change the extension of your DOCX to ZIP, then look for the subfolders 'word' and then 'activeX')

using (MemoryStream ms = new MemoryStream())
    {
        //open the template doc using OpenOfficeSDK
        using (var mainDoc = WordprocessingDocument.Open("myDoc.docx", false))
        //creat a new Word doc in memory using the SDK
        using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document, true))
        {
            //copy all of the "Parts" of the template to our new doc
            foreach (var part in mainDoc.Parts)
            {
                wordDocument.AddPart(part.OpenXmlPart, part.RelationshipId);
            }

            //copy the "Body" of the "MainDocumentPart" of the template to our new doc
            Body body = wordDocument.MainDocumentPart.Document.Body;

            //find our radio button
            var radioButton = body.Descendants<Control>().First();

            var embeddedPart = wordDocument.MainDocumentPart.GetPartById(control.Id) as EmbeddedControlPersistencePart;

            //get the XML for the embedded part from wherever it is stored
            var xreader = OpenXmlReader.Create(embeddedPart.GetStream());
            xreader.Read();
            var xml = xreader.LoadCurrentElement();
            var classID = xml.GetAttribute("classid", xml.NamespaceUri).Value;
            if (classID == "{8BD21D50-EC42-11CE-9E0D-00AA006002F3}") //it is a radio
            {
                var binary = embeddedPart.EmbeddedControlPersistenceBinaryDataParts.First();

                using (var source = binary.GetStream())
                {
                    using (var reader = new BinaryReader(source))
                    {
                        using (var stream = new MemoryStream(reader.ReadBytes((int)source.Length)))
                        {
                            stream.Position = 2080; //this is position 00000800 in the BIN file.
                            var r = (byte)stream.ReadByte();

                            while (r != 48)
                            {
                                r = (byte)stream.ReadByte(); //search for decimal 48, which is the value of an 'Off' radio button
                            }

                            if (r == 48)
                            {
                                stream.Seek(-1, SeekOrigin.Current);
                                stream.WriteByte(0x31); //change the value to hex 31, which is 'On'
                                                        //save the modified value to the stream
                                stream.Seek(0, SeekOrigin.Begin);
                                binary.FeedData(stream);
                            }
                        }
                    }
                }
            }

        }
    }

I used the OffVis tool to inspect the ActiveX BIN files. I was able to ascertain that the meaningful info starts at position 2080 in the BIN stream. This is a screenshot from OffVis:

来自 OffVis 的屏幕截图

CShark has already told us that the hex value of an unchecked radio button is 0x30, and 0x31 for checked. I simply loop from position 2080 until the unchecked value is found (0x30 = 48 in decimal). (You can see it with the red circle in the picture). This value can then be changed to 0x31 (checked) and saved back to the stream. I accept this is not perfectly robust, but it is a start. Obviously you will need to set your own position depending on your control.

Finally, the stream can be made use of as you wish. I convert mine to a byte array and save to a DB.

In conclusion, we have copied a DOCX with ActiveX controls into a new DOCX, modified the checked/unchecked properties of an ActiveX radio, and saved the new document.

I hope this proves useful to someone. Please don't shoot me, this is the first time I have modified a binary file on the fly!

Once again, thanks CShark!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM