表单控件是与excel早期版本兼容的原始控件,通过表单控件,用户可以快速地将数据填写到模板文档中,如果你想在不使用vba代码的情况下轻松引用单元格数据并与其进行交互,也可使用表单控件。本文将介绍如何使用spire.xls给excel表格添加常见的表单控件例如文本框、单选按钮、复选框和组合框,以及如何删除现有的表单控件。
插入表单控件
c#
//创建workbook实例
workbook workbook = new workbook();
//添加工作表
worksheet sheet = workbook.worksheets[0];
//插入文本框
sheet.range["a2"].text = "姓名:";
itextboxshape textbox = sheet.textboxes.addtextbox(2, 2, 18, 65);
textbox.text = "肖恩";
textbox.fill.forecolor = color.greenyellow;
textbox.halignment = commenthaligntype.center;
textbox.valignment = commentvaligntype.center;
//插入单选按钮
sheet.range["a4"].text = "性别:";
iradiobutton radiobutton = sheet.radiobuttons.add(4, 2, 18, 65);
radiobutton.checkstate = checkstate.checked;
radiobutton.text = "男";
radiobutton = sheet.radiobuttons.add(4, 4, 18, 65);
radiobutton.text = "女";
//插入复选框
sheet.range["a6"].text = "爱好:";
icheckbox checkbox = sheet.checkboxes.addcheckbox(6, 2, 18, 65);
checkbox.checkstate = checkstate.checked;
checkbox.text = "摄影";
checkbox = sheet.checkboxes.addcheckbox(6, 4, 18, 65);
checkbox.text = "唱歌";
checkbox = sheet.checkboxes.addcheckbox(6, 6, 18, 65);
checkbox.checkstate = checkstate.checked;
checkbox.text = "旅行";
//插入组合框
sheet["a8"].text = "职业:";
sheet["a9"].text = "学生";
sheet["a10"].text = "教师";
sheet["a11"].text = "医生";
icomboboxshape combobox = sheet.comboboxes.addcombobox(8, 2, 18, 65);
combobox.listfillrange = sheet["a9:a11"];
combobox.linkedcell = sheet.range["c8"];
combobox.selectedindex = 2;
//保存文档
workbook.savetofile("addformcontrols.xlsx", excelversion.version2010);
vb.net
'创建workbook实例
dim workbook as new workbook()
'添加工作表
dim sheet as worksheet = workbook.worksheets(0)
'插入文本框
sheet.range("a2").text = "姓名:"
dim textbox as itextboxshape = sheet.textboxes.addtextbox(2, 2, 18, 65)
textbox.text = "肖恩"
textbox.fill.forecolor = color.greenyellow
textbox.halignment = commenthaligntype.center
textbox.valignment = commentvaligntype.center
'插入单选按钮
sheet.range("a4").text = "性别:"
dim radiobutton as iradiobutton = sheet.radiobuttons.add(4, 2, 18, 65)
radiobutton.checkstate = checkstate.checked
radiobutton.text = "男"
radiobutton = sheet.radiobuttons.add(4, 4, 18, 65)
radiobutton.text = "女"
'插入复选框
sheet.range("a6").text = "爱好:"
dim checkbox as icheckbox = sheet.checkboxes.addcheckbox(6, 2, 18, 65)
checkbox.checkstate = checkstate.checked
checkbox.text = "摄影"
checkbox = sheet.checkboxes.addcheckbox(6, 4, 18, 65)
checkbox.text = "唱歌"
checkbox = sheet.checkboxes.addcheckbox(6, 6, 18, 65)
checkbox.checkstate = checkstate.checked
checkbox.text = "旅行"
'插入组合框
sheet("a8").text = "职业:"
sheet("a9").text = "学生"
sheet("a10").text = "教师"
sheet("a11").text = "医生"
dim combobox as icomboboxshape = sheet.comboboxes.addcombobox(8, 2, 18, 65)
combobox.listfillrange = sheet("a9:a11")
combobox.linkedcell = sheet.range("c8")
combobox.selectedindex = 2
'保存文档
workbook.savetofile("addformcontrols.xlsx", excelversion.version2010)
删除表单控件
c#
//创建workbook实例
workbook workbook = new workbook();
//载入excel文档
workbook.loadfromfile("addformcontrols.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//删除工作表中所有的复选框
for (int i = 0; i < sheet.checkboxes.count; i )
{
sheet.checkboxes[i].remove();
}
//保存文档
workbook.savetofile("removecheckboxes.xlsx", excelversion.version2010);
vb.net
'创建workbook实例
dim workbook as new workbook()
'载入excel文档
workbook.loadfromfile("addformcontrols.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'删除工作表中所有的复选框
for i as integer = 0 to sheet.checkboxes.count - 1
sheet.checkboxes(i).remove()
next
'保存文档
workbook.savetofile("removecheckboxes.xlsx", excelversion.version2010)