Ava add and remove Excel form controls
Form controls allow users to quickly populate template documents with data and easily reference and interact with cell data. This article introduces how to add form controls to Excel tables, including text boxes, radio buttons, check boxes, combo boxes, spinner buttons, etc., and how to delete specified form controls in Excel through Java code examples.
Program running environment: Java, IDEA, jdk1.8.0, no need to install Microsoft Excel
Tools: Free Spire.XLS for Java (Free Edition)
Java Example 1: Adding a Form Control
import com.spire.xls.*; import com.spire.xls.core.*; public class AddFormControl { public static void main(String[] args) { //Create workbook, get first worksheet Workbook wb = new Workbook(); Worksheet sheet = wb.getWorksheets().get(0); sheet.getCellRange("A2").setText("Name: "); //add text box ITextBoxShape textbox = sheet.getTextBoxes().addTextBox(2, 2, 18, 65); textbox.setText("Li Hong"); textbox.setHAlignment(CommentHAlignType.Center); textbox.setVAlignment(CommentVAlignType.Center); sheet.getCellRange("A4").setText("Gender: "); //Add radio button 1 IRadioButton radiobutton1 = sheet.getRadioButtons().add(4, 2, 18, 65); radiobutton1.setText("male"); radiobutton1.setCheckState(CheckState.Checked); //Add radio button 2 IRadioButton radiobutton2 = sheet.getRadioButtons().add(4, 4, 18, 65); radiobutton2.setText("female"); sheet.getCellRange("A6").setText("Hobby: "); //Add checkbox 1 ICheckBox checkbox1 = sheet.getCheckBoxes().addCheckBox(6, 2, 18, 65); checkbox1.setCheckState(CheckState.Checked); checkbox1.setText("Photo"); //Add checkbox 2 ICheckBox checkbox2 = sheet.getCheckBoxes().addCheckBox(6, 4, 18, 65); checkbox2.setCheckState(CheckState.Checked); checkbox2.setText("Go"); sheet.getCellRange("A8").setText("Occupation: "); sheet.getCellRange("A20").setText("Student"); sheet.getCellRange("A21").setText("Teacher"); sheet.getCellRange("A22").setText("Doctor"); //Add combo box IComboBoxShape combobox = sheet.getComboBoxes().addComboBox(8, 2, 18, 65); combobox.setListFillRange(sheet.getCellRange("A20:A22")); combobox.setSelectedIndex(2); sheet.getCellRange("A10").setText("Executive Level: "); //Add a fine adjustment button ISpinnerShape spinnerShape = sheet.getSpinnerShapes().addSpinner(10,2,18,30); spinnerShape.setCurrentValue(1); spinnerShape.setDisplay3DShading(true); spinnerShape.setLinkedCell(sheet.getCellRange("B10")); spinnerShape.setMin(1); spinnerShape.setMax(5); //Save document wb.saveToFile("AddControls.xlsx", ExcelVersion.Version2013); wb.dispose(); }}