Programmatically create checkboxes in c# in an excel spreadsheet

后端 未结 3 2125
无人及你
无人及你 2020-12-19 14:29

As part of a project I am working on, I a need to be able to create checkboxes inside an excel spreadsheet, could anyone provide a simple example or direct me to a useful re

相关标签:
3条回答
  • 2020-12-19 15:10

    Interesting question. I haven't tried it, but I think you either add it as a Worksheet.Shape item, or add it as an OLEObject. The following example shows how to read an existing checkbox from a worksheet. It is probably a good starting place to work backwards from:

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

    0 讨论(0)
  • 2020-12-19 15:27

    here is what I used for c#

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

    I used cell.Left + 1, cell.Top + 1, cell.Width - 2, cell.Height - 2 to maintain borders in four sides of the cell

    0 讨论(0)
  • 2020-12-19 15:33

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

    In general, the two types of controls are similar in that they work the same way. That is, a textbox from the Drawing toolbar is a control in which you can type text. That’s fundamentally the same as the ActiveX textbox. Checkboxes of either type are used to select or deselect an option.

    Obviously, a decision to use one or the other will based on their differences, not their similarities. The primary differences, for me anyway, are events and formatting. ActiveX controls give the spreadsheet designer a lot more flexibility when it comes to displaying or reacting to events of controls. Viewing the properties of an ActiveX control demonstrates how much more you can do with them than with Forms controls. Also, Forrms controls have one basic event, the click event, which you access by assigning a macro to the control. ActiveX controls, on the other hand, have a lot of events. The ActiveX combobox, for instance, has fifteen events.

    All that flexibility comes at a cost (you knew it would). ActiveX controls carry a lot more overhead with them and have a history of being buggy.

    Weighing the good and the bad, I always use Forms controls unless I have a specific reason to use an ActiveX. Forms controls are lean and mean and simple to use. If, however, I need a combobox to display a certain font, I have no choice but to use an ActiveX control. Similarly, if I need to avoid certain characters in a textbox, I’ll need the KeyPress event which is not available from the Forms textbox. So my advice, and my practice, is to use Forms controls unless I need functionality that only ActiveX controls offer.

    Forms Checkbox add code

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

    ActiveX Checkbox add code

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

    This code is VBA but can easily be invoked from .Net via interop

    0 讨论(0)
提交回复
热议问题