This article guides on using ActiveX controls in Excel with Java. It shares the details of writing the application and a sample code for inserting Excel Active X control with Java. You will learn to add different ActiveX controls and access ActiveX controls in an existing Excel file for reading and updating the properties.
Steps to Insert ActiveX Controls in Excel with Java
- Set IDE to use Aspose.Cells for Java to add ActiveX controls
- Create a workbook and add a button to the shapes collection of the target sheet in the workbook
- Add another ComboBox ActiveX control in the shapes collection
- Link both the controls with separate cells
- Set the value for the text in the ComboBox
- Access an ActiveX control and update its properties
- Save the final workbook
These steps describe how to use ActiveX controls in Excel with Java. Create a new workbook, access a sheet, access the shapes collection in the selected sheet, and add as many controls as required by calling the addActiveXControl() method by providing the control type, location, size, and specific properties of the respective. You can access the ActiveX controls by parsing the shapes collection in the sheet and manipulating the desired controls by checking their type.
Code to Add Active X for Excel with Java
import com.aspose.cells.*; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Use ActiveX control | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
// Create workbook object and add a command button | |
Workbook wb = new Workbook(); | |
Worksheet sheet = wb.getWorksheets().get(0); | |
// Add a Button | |
Shape button = sheet.getShapes().addActiveXControl(ControlType.COMMAND_BUTTON, 3, 0, 3, 0, 90, 25); | |
// Access the ActiveX control for setting its properties | |
ActiveXControl buttonControl = button.getActiveXControl(); | |
buttonControl.setLinkedCell("A1"); | |
// Add a ComboBox | |
Shape combobox = sheet.getShapes().addActiveXControl(ControlType.COMBO_BOX, 6, 0, 6, 0, 90, 25); | |
// Customize the ActiveX control | |
ActiveXControl c1 = combobox.getActiveXControl(); | |
c1.setLinkedCell("A4"); | |
ComboBoxActiveXControl comboControl = (ComboBoxActiveXControl)c1; | |
comboControl.setValue("Sample original value"); | |
// Find and update the ActiveX control | |
for (Object obj : sheet.getShapes()) | |
{ | |
Shape shp = (Shape)obj; | |
if (shp.getActiveXControl() != null) | |
{ | |
ActiveXControl control = shp.getActiveXControl(); | |
if (control.getType() == ControlType.COMBO_BOX) | |
{ | |
ComboBoxActiveXControl comboBoxActiveX = (ComboBoxActiveXControl)control; | |
comboBoxActiveX.setValue("Updated Value"); | |
} | |
} | |
} | |
// Save the output | |
wb.save("AddActiveXControls_out.xlsx", SaveFormat.XLSX); | |
System.out.println("Done"); | |
} | |
} |
This sample code demonstrates the usage of Active X controls in Excel with Java. The ControlType enumerator contains multiple control types that you can use to create different controls in an Excel sheet. You can set properties like setting the flag to make the control visible, setting shadow, selecting the mouse pointer for the control, and enabling/disabling state, color, and fonts.
This article has guided us to insert and access ActiveX controls in Excel with Java. To insert charts in an Excel sheet, refer to the article on How to create Excel chart in Java.