Search code examples
c#excelcheckboxradio-buttoncell

Excel Data Manipulation


I am able to open up a pre-set excel document (.xls/.xlsx) and change particular cells using:

Microsoft.Office.Interop.Excel.Range date = theWorkSheet1.UsedRange;
date = date.get_Range("B4");
date.Value = theDate;

However, this only allows me to change the specified cell values (in this case B4). I have come across Radio Buttons and Check Boxes and would like to change those values in the current excel sheet using my GUI with custom functionality. So if something in my C# GUI is checked or enabled, the radio buttons/check boxes in the excel file will change accordingly.

There are more than one set of radio buttons and check boxes.

Can anyone help me figure out how to read the different groups of radio buttons and check boxes and properly populate them according to a set of rules?


Solution

  • The Worksheet object will have properties on it called CheckBoxes and OptionButtons. Each of these is a collection of all the checkboxes and option buttons on the worksheet, respectively. You can set their Value property to 0 (unchecked), 1 (checked) or 2 (indeterminate), for example:

    worksheet.Checkboxes("Checkbox1").Value = 1

    Some worksheets may have OLEObjects on them instead of plain old Excel Checkboxes, in which case you'll have to do this:

    worksheet.OLEObjects("Checkbox1").Object.Value = 1