C# 添加Excel表单控件(Form Controls)
程序员文章站
2022-12-16 13:29:38
在Excel中,添加的控件可以和单元格关联,我们可以操作控件来修改单元格的内容,在下面的文章中,将介绍在Excel中添加几种不同的表单控件的方法,包括: 添加文本框(Textbox) 单选按钮(Radio button) 复选框(Checkbox) 组合框(combo Box) 使用工具 Free ......
在excel中,添加的控件可以和单元格关联,我们可以操作控件来修改单元格的内容,在下面的文章中,将介绍在excel中添加几种不同的表单控件的方法,包括:
- 添加文本框(textbox)
- 单选按钮(radio button)
- 复选框(checkbox)
- 组合框(combo box)
使用工具
ps:下载安装该组件后,注意在项目程序中添加引用spire.xls.dll(dll文件可在安装路径下的bin文件夹中获取),如下图所示
代码示例
【示例1】插入excel表单控件
步骤1:创建工作表
//实例化一个workbook类实例,并获取第1个工作表 workbook workbook = new workbook(); worksheet sheet = workbook.worksheets[0]; //设置表格行高、列宽 sheet.range["a1:f1"].columnwidth = 15f; sheet.range["a1:b12"].rowheight = 20f;
步骤 2:插入文本框
//插入文本框控件,指定文本框位置、大小以及文本对齐方式 sheet.range["a1"].text = "姓名:"; itextboxshape textbox = sheet.textboxes.addtextbox(1, 2, 25, 110); textbox.text = "john"; textbox.halignment = commenthaligntype.center; textbox.valignment = commentvaligntype.center;
步骤 3:插入单选按钮
//插入单选按钮,指定单元格位置 sheet.range["a3"].text = "性别:"; iradiobutton radiobutton = sheet.radiobuttons.add(3, 2, 20, 80); radiobutton.checkstate = checkstate.checked; radiobutton.text = "女"; radiobutton = sheet.radiobuttons.add(3, 3, 20, 80); radiobutton.text = "男";
步骤 4:插入复选框
//插入复选框并指定单元格位置 sheet.range["a5"].text = "所在行业:"; icheckbox checkbox = sheet.checkboxes.addcheckbox(5, 2, 18, 65); checkbox.checkstate = checkstate.checked; checkbox.text = "教育"; checkbox = sheet.checkboxes.addcheckbox(5, 3, 18, 65); checkbox.text = "医疗"; checkbox = sheet.checkboxes.addcheckbox(5, 4, 18, 65); checkbox.text = "it"; checkbox = sheet.checkboxes.addcheckbox(5, 5, 18, 65); checkbox.text = "零售"; checkbox = sheet.checkboxes.addcheckbox(5, 6, 18, 65); checkbox.text = "其他";
步骤 5:插入组合框
//插入组合框,并指定单元格位置、大小 sheet["a7"].text = "年龄(段):"; sheet["a8"].text = "<18"; sheet["a9"].text = "18<y<30"; sheet["a10"].text = "30<y<50"; icomboboxshape combobox = sheet.comboboxes.addcombobox(7, 2, 23, 100); combobox.listfillrange = sheet["a8:a10"];
步骤 6:指定combox的关联单元格
sheet["a12"].text = "代表人群类别:"; combobox.linkedcell = sheet.range["b12"]; combobox.selectedindex = 1;
步骤 7:保存文档
workbook.savetofile("addformcontrols.xlsx", excelversion.version2010);
运行该项目程序,生成文件(可在项目文件夹bin>debug下查看文档)
全部代码:
using spire.xls; using spire.xls.core; using system.drawing; namespace formcontrols_xls { class program { static void main(string[] args) { //实例化一个workbook类实例,并获取第1个工作表 workbook workbook = new workbook(); worksheet sheet = workbook.worksheets[0]; //设置表格行高、列宽 sheet.range["a1:f1"].columnwidth = 15f; sheet.range["a1:b12"].rowheight = 20f; //插入文本框控件,指定文本框位置、大小以及文本对齐方式 sheet.range["a1"].text = "姓名:"; itextboxshape textbox = sheet.textboxes.addtextbox(1, 2, 25, 110); textbox.text = "john"; textbox.halignment = commenthaligntype.center; textbox.valignment = commentvaligntype.center; //插入单选按钮,指定单元格位置 sheet.range["a3"].text = "性别:"; iradiobutton radiobutton = sheet.radiobuttons.add(3, 2, 20, 80); radiobutton.checkstate = checkstate.checked; radiobutton.text = "女"; radiobutton = sheet.radiobuttons.add(3, 3, 20, 80); radiobutton.text = "男"; //插入复选框并指定单元格位置 sheet.range["a5"].text = "所在行业:"; icheckbox checkbox = sheet.checkboxes.addcheckbox(5, 2, 18, 65); checkbox.checkstate = checkstate.checked; checkbox.text = "教育"; checkbox = sheet.checkboxes.addcheckbox(5, 3, 18, 65); checkbox.text = "医疗"; checkbox = sheet.checkboxes.addcheckbox(5, 4, 18, 65); checkbox.text = "it"; checkbox = sheet.checkboxes.addcheckbox(5, 5, 18, 65); checkbox.text = "零售"; checkbox = sheet.checkboxes.addcheckbox(5, 6, 18, 65); checkbox.text = "其他"; //插入组合框,并指定单元格位置、大小 sheet["a7"].text = "年龄(段):"; sheet["a8"].text = "<18"; sheet["a9"].text = "18<y<30"; sheet["a10"].text = "30<y<50"; icomboboxshape combobox = sheet.comboboxes.addcombobox(7, 2, 23, 100); combobox.listfillrange = sheet["a8:a10"]; //指定组合框的关联单元格 sheet["a12"].text = "代表人群类别:"; combobox.linkedcell = sheet.range["b12"]; combobox.selectedindex = 1; //保存文档 workbook.savetofile("addformcontrols.xlsx", excelversion.version2010); system.diagnostics.process.start("addformcontrols.xlsx"); } } }
【示例 2】 删除excel表单控件
步骤 1:加载文档,并获取指定单元格
workbook workbook = new workbook(); workbook.loadfromfile("test.xlsx"); worksheet sheet = workbook.worksheets[0];
步骤 2:删除组合框
for (int i = 0; i < sheet.comboboxes.count; i++) { sheet.comboboxes[i].remove(); }
步骤 3:保存文档
workbook.savetofile("removecomboboxes.xlsx", excelversion.version2010);
全部代码:
using spire.xls; namespace removeformcontrol_xls { class program { static void main(string[] args) { //创建workbook实例,加载excel文档 workbook workbook = new workbook(); workbook.loadfromfile("test.xlsx"); //获取第一个工作表 worksheet sheet = workbook.worksheets[0]; //删除工作表中所有的组合框 for (int i = 0; i < sheet.comboboxes.count; i++) { sheet.comboboxes[i].remove(); } //保存并打开文档 workbook.savetofile("removecomboboxes.xlsx", excelversion.version2010); system.diagnostics.process.start("removecomboboxes.xlsx"); } } }
运行程序后,表格中相应的控件将被删除。
以上是本次关于c#操作excel表单控件的全部内容,本文完。
(如需转载,请注明出处)