Home >Java >javaTutorial >How to add and delete controls to an Excel table in Java?

How to add and delete controls to an Excel table in Java?

王林
王林forward
2023-04-27 17:58:081056browse

Introduction

Through the form control, users can quickly fill in data into the template document and easily reference and interact with cell data.

Program running environment: Java, IDEA, jdk1.8.0, no need to install Microsoft Excel

Using tools: Free Spire.XLS for Java ( Free version)

jar acquisition and import: Download the jar package from the official website, decompress it, and import the jar file in the lib folder into the java program. Or you can download it from the maven repository and import it into the Maven project. Import the effect as follows:

How to add and delete controls to an Excel table in Java?

Java Example 1 Add form control

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

public class AddFormControl {
    public static void main(String[] args) {
        //创建工作簿,获取第一个工作表
        Workbook wb = new Workbook();
        Worksheet sheet = wb.getWorksheets().get(0);

        sheet.getCellRange("A2").setText("姓名: ");
        //添加文本框
        ITextBoxShape textbox = sheet.getTextBoxes().addTextBox(2, 2, 18, 65);
        textbox.setText("李宏");
        textbox.setHAlignment(CommentHAlignType.Center);
        textbox.setVAlignment(CommentVAlignType.Center);

        sheet.getCellRange("A4").setText("性别: ");
        //添加单选按钮1
        IRadioButton radiobutton1 = sheet.getRadioButtons().add(4, 2, 18, 65);
        radiobutton1.setText("男");
        radiobutton1.setCheckState(CheckState.Checked);
        //添加单选按钮2
        IRadioButton radiobutton2 = sheet.getRadioButtons().add(4, 4, 18, 65);
        radiobutton2.setText("女");

        sheet.getCellRange("A6").setText("爱好:");
        //添加复选框1
        ICheckBox checkbox1 = sheet.getCheckBoxes().addCheckBox(6, 2, 18, 65);
        checkbox1.setCheckState(CheckState.Checked);
        checkbox1.setText("摄影");
        //添加复选框2
        ICheckBox checkbox2 = sheet.getCheckBoxes().addCheckBox(6, 4, 18, 65);
        checkbox2.setCheckState(CheckState.Checked);
        checkbox2.setText("围棋");

        sheet.getCellRange("A8").setText("职业:");
        sheet.getCellRange("A20").setText("学生");
        sheet.getCellRange("A21").setText("教师");
        sheet.getCellRange("A22").setText("医生");
        //添加组合框
        IComboBoxShape combobox = sheet.getComboBoxes().addComboBox(8, 2, 18, 65);
        combobox.setListFillRange(sheet.getCellRange("A20:A22"));
        combobox.setSelectedIndex(2);

        sheet.getCellRange("A10").setText("行政级别:");
        //添加微调按钮
        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);

        //保存文档
        wb.saveToFile("AddControls.xlsx", ExcelVersion.Version2013);
        wb.dispose();
    }
}

Add effect of form control:

How to add and delete controls to an Excel table in Java?

Java Example 2 Deleting form controls

import com.spire.xls.*;

public class RemoveFormControl {
    public static void main(String[] args) {
        //加载Excel工作簿
        Workbook wb = new Workbook();
        wb.loadFromFile("AddControls.xlsx");

        //获取第一个工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //删除工作表中的所有单选按钮
        for(int j = 0; j < sheet.getRadioButtons().getCount(); j ++){
            sheet.getRadioButtons().get(j).remove();
        }

        //保存文档
        wb.saveToFile("RemoveFormControl.xlsx",ExcelVersion.Version2013);
        wb.dispose();
    }
}

Deletion effect of form controls:

How to add and delete controls to an Excel table in Java?

The above is the detailed content of How to add and delete controls to an Excel table in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete