Last active
June 15, 2024 13:00
-
-
Save aspose-com-kb/b712a41e2d0106ae0e27be9d78c6ac3f to your computer and use it in GitHub Desktop.
Using ActiveX Controls in Excel with C#. For more details: https://kb.aspose.com/cells/net/using-activex-controls-in-excel-with-csharp/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Aspose.Cells; | |
using Aspose.Cells.Drawing; | |
using Aspose.Cells.Drawing.ActiveXControls; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
new License().SetLicense("License.lic"); | |
// Create workbook object | |
Workbook wb = new Workbook(); | |
// Access first worksheet | |
Worksheet sheet = wb.Worksheets[0]; | |
// Add Command Button ActiveX Control inside the Shape Collection | |
Shape s = sheet.Shapes.AddActiveXControl(ControlType.CommandButton, 4, 0, 4, 0, 100, 30); | |
// Access the ActiveX control object and set its linked cell property | |
ActiveXControl c = s.ActiveXControl; | |
c.LinkedCell = "A1"; | |
// Add Toggle Button ActiveX Control inside the Shape Collection | |
Shape s1 = sheet.Shapes.AddActiveXControl(ControlType.ComboBox, 16, 0, 4, 0, 100, 30); | |
// Access the ActiveX control object and set its linked cell property | |
ActiveXControl c1 = s1.ActiveXControl; | |
c1.LinkedCell = "A4"; | |
ComboBoxActiveXControl comboControl = (ComboBoxActiveXControl)c1; | |
comboControl.Value = "A sample value for the ComboBox"; | |
// Save the workbook | |
wb.Save("Combo box with original value.xlsx"); | |
foreach (var shape in sheet.Shapes) | |
{ | |
// Access specific ActiveX Control and set its value | |
if (shape.ActiveXControl != null) | |
{ | |
// Access Shape ActiveX Control | |
ActiveXControl control = shape.ActiveXControl; | |
// Check for the target type | |
if (control.Type == ControlType.ComboBox) | |
{ | |
// Type cast ActiveXControl into ComboBoxActiveXControl and change its value | |
ComboBoxActiveXControl comboBoxActiveX = (ComboBoxActiveXControl)control; | |
comboBoxActiveX.Value = "A new value for the ComboBox"; | |
} | |
} | |
} | |
// Save the workbook in xlsx format | |
wb.Save("AddActiveXControls_out.xlsx", SaveFormat.Xlsx); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment