Programmatically create checkboxes in C # in an Excel spreadsheet

As part of the project I'm working on, I need to be able to create checkboxes inside an Excel spreadsheet, can someone provide a simple example or send me a useful link? I am using excel Interop.

Thanks in advance.

+3
source share
3 answers

There are two types of controls that you can apply to a worksheet, ActiveX controls from the Control Toolbox and Forms elements on the Forms (or Drawing) toolbar.

, , . "" , . , ActiveX. .

, , . . ActiveX , . ActiveX , , Forms. , Forrms - , , . , ActiveX . , ActiveX .

( , ). ActiveX .

, Forms, ActiveX. , . , , , , , ActiveX. , , KeyPress, Forms. - Forms, , ActiveX.

Checkbox

 ActiveSheet.CheckBoxes.Add 87, 18, 72, 17.25

ActiveX

    ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1", Link:=False, _
    DisplayAsIcon:=False, Left:=69.75, Top:=59.25, Width:=117.75, Height _
    :=14.25)

VBA, .Net interop

+6

#

OLEObjects objs = worksheet.OLEObjects();
OLEObject obj = objs.Add("Forms.CheckBox.1", System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, cell.Left + 1, cell.Top + 1, cell.Width - 2, cell.Height - 2);
obj.Object.Caption = "";
if (text == "TRUE")
{
    obj.Object.value = true;
}
else
{
    obj.Object.value = false;
}

cell.Left + 1, cell.Top + 1, cell.Width - 2, cell.Height - 2

0

Interest Ask. I have not tried it, but I think you will either add it as a Worksheet.Shape element or add it as an OLEObject. The following example shows how to read an existing checkbox in a worksheet. This is probably a good starting place to work backwards:

http://forums.asp.net/p/1244356/3293168.aspx

-1
source

Source: https://habr.com/ru/post/1712810/


All Articles