标签:tps set spinner 组合框 导入 ksh version getch comment
通过表单控件,用户可以快速地将数据填写到模板文档中,轻松引用单元格数据并与其进行交互。本文通过Java代码示例介绍如何在Excel表格中添加表单控件,包括文本框、单选按钮、复选框、组合框、微调按钮等;以及如何删除Excel中的指定表单控件。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();
}
}
表单控件添加效果:
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();
}
}
表单控件删除效果:
(本文完)
标签:tps set spinner 组合框 导入 ksh version getch comment
原文地址:https://blog.51cto.com/eiceblue/2483488