DEV Community

CodeSharing
CodeSharing

Posted on

1 1

Java/ Add and Remove Form Controls in Excel

In an Excel worksheet, form controls such as check boxes, option buttons and combo boxes can make data entry easier. The following examples will show you how to add and remove text box, option button, check box and combo box form controls using Free Spire.XLS for Java.

Installation
Method 1: Download the free API and unzip it, then add the Spire.Xls.jar file to your project as dependency.
Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>3.9.1</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Add Form Controls

import com.spire.xls.*;
import com.spire.xls.core.*;
import java.awt.*;

public class AddFormControls {
    public static void main(String[] args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        sheet.getCellRange("A2").setText("Name: ");
        //Add a text box
        ITextBoxShape textbox = sheet.getTextBoxes().addTextBox(2, 2, 18, 65);
        textbox.setText("Jessie");
        textbox.getFill().setForeColor(Color.PINK);
        textbox.setHAlignment(CommentHAlignType.Center);
        textbox.setVAlignment(CommentVAlignType.Center);

        sheet.getCellRange("A4").setText("Gender: ");
        //Add an option button
        IRadioButton radiobutton1 = sheet.getRadioButtons().add(4, 2, 18, 65);
        radiobutton1.setText("Male");
        radiobutton1.setCheckState(CheckState.Checked);
        //Add an option button
        IRadioButton radiobutton2 = sheet.getRadioButtons().add(4, 4, 18, 65);
        radiobutton2.setText("Female");

        sheet.getCellRange("A6").setText("Hobby: ");
        //Add a check box
        ICheckBox checkbox1 = sheet.getCheckBoxes().addCheckBox(6, 2, 18, 100);
        checkbox1.setCheckState(CheckState.Checked);
        checkbox1.setText("Photography");
        //Add a check box
        ICheckBox checkbox2 = sheet.getCheckBoxes().addCheckBox(6, 4, 18, 65);
        checkbox2.setCheckState(CheckState.Checked);
        checkbox2.setText("Travel");

        sheet.getCellRange("A8").setText("Profession: ");
        sheet.getCellRange("A20").setText("Student");
        sheet.getCellRange("A21").setText("Teacher");
        sheet.getCellRange("A22").setText("Doctor");
        //Add a combo box
        IComboBoxShape combobox = sheet.getComboBoxes().addComboBox(8, 2, 18, 65);
        combobox.setListFillRange(sheet.getCellRange("A20:A22"));
        combobox.setSelectedIndex(1);

        for (int column = 1; column < 5; column ++)
        {
            sheet.setColumnWidth(column, 15f);
        }

        //Save the file
        workbook.saveToFile("AddControls.xlsx", ExcelVersion.Version2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Remove Form Controls

import com.spire.xls.*;

public class RemoveFormControls {
    public static void main(String[] args){
        //Load an Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("AddControls.xlsx");
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Remove option buttons from the worksheet
        for(int j = 0; j < sheet.getRadioButtons().getCount(); j ++){
            sheet.getRadioButtons().get(j).remove();
        }

        //Remove check boxes from the worksheet
        for(int i = 0; i < sheet.getCheckBoxes().getCount(); i ++){
            sheet.getCheckBoxes().get(i).remove();
        }

        //Save the file
        workbook.saveToFile("RemoveControls.xlsx", ExcelVersion.Version2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs