Table of Contents

Form controls

XlsxDocumentSheet.Controls lists a sheet's form controls: check boxes, option buttons, combo and list boxes, buttons, scroll bars and spinners, each with the cell it is linked to, the range it lists, its caption, its value and where it is. A control is added as Excel's Developer > Insert adds one, and removed. Measured against Excel: listed as Excel lists them, and controls added here opened and listed by Excel with their kind, cell link, list and caption.

dotnet add package DocWright.Spreadsheet

List a sheet's controls

using DocWright.Spreadsheet;

XlsxDocument workbook = XlsxDocument.Open(File.OpenRead("sales-objects.xlsx"));
XlsxFormControls controls = workbook.FindSheet("Objects")!.Controls;
foreach (XlsxFormControl control in controls)
{
    Console.WriteLine($"{control}: list {control.ItemsRange ?? "-"}, value {control.Value?.ToString() ?? "-"}, at {control.Anchor?.From}");
}

Value is a check box's 1 or 0, a list's selected position (one-based, 0 for none), or a scroll bar's or spinner's value. A control also writes its state to its linked cell, which reads as an ordinary cell value.

Add and remove a control

using DocWright.Spreadsheet;

XlsxFormControl agree = controls.Add(XlsxFormControlKind.CheckBox, "N2", new XlsxFormControlOptions { LinkedCell = "O2", Text = "Agree" });
XlsxFormControl region = controls.Add(XlsxFormControlKind.ComboBox, "N4", new XlsxFormControlOptions { LinkedCell = "O4", ItemsRange = "Data!A2:A6" });
Console.WriteLine($"added {agree} and {region}");
controls.Find("Go")!.Remove();
Check (CheckBox, linked to K2): list -, value 0, at I14 +200025 +57150
Option (OptionButton): list -, value 0, at I16 +200025 +0
Combo (ComboBox, linked to K3): list Data!A2:A6, value 0, at I17 +200025 +123825
List (ListBox, linked to K4): list Data!A2:A6, value 0, at I19 +200025 +57150
Go (Button): list -, value -, at K14 +504825 +57150
Scroll (ScrollBar, linked to K5): list -, value 0, at K16 +504825 +123825
Spin (Spinner, linked to K6): list -, value 0, at K18 +504825 +57150
added Check Box 8 (CheckBox, linked to O2) and Drop Down 9 (ComboBox, linked to O4)

An unnamed control gets Excel's name for its kind and the next free number. Excel keeps a control in four places (the sheet's control list, a properties part, the drawing and the legacy VML drawing); Add writes all four, making a drawing when the sheet has none, and Remove takes it out of all four. Group boxes, labels and ActiveX controls are kept as they are, not added. Download the workbook: sales-controls.xlsx.

Note

Form controls are kept and edited, but not drawn when a sheet is converted to PDF or images.