C# 添加Excel表单控件(Form Controls)
发布时间:2020-12-16 01:09:56 所属栏目:百科 来源:网络整理
导读:在Excel中,添加的控件可以和单元格关联,我们可以操作控件来修改单元格的内容,在下面的文章中,将介绍在Excel中添加几种不同的表单控件的方法,包括: ?添加文本框(Textbox) ?单选按钮(Radio button) ?复选框(Checkbox) ?组合框(combo Box) 使用工
在Excel中,添加的控件可以和单元格关联,我们可以操作控件来修改单元格的内容,在下面的文章中,将介绍在Excel中添加几种不同的表单控件的方法,包括:
使用工具
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,1)">25,1)">110); textBox.Text = John; textBox.HAlignment = CommentHAlignType.Center; textBox.VAlignment = CommentVAlignType.Center; 步骤 3:插入单选按钮 插入单选按钮,指定单元格位置 sheet.Range[A3性别:; IRadioButton radioButton = sheet.RadioButtons.Add(3,1)">20,1)">80); radioButton.CheckState = CheckState.Checked; radioButton.Text = 女; radioButton = sheet.RadioButtons.Add(); radioButton.Text = 男"; 步骤 4:插入复选框 插入复选框并指定单元格位置 sheet.Range[A5所在行业:; ICheckBox checkBox = sheet.CheckBoxes.AddCheckBox(5,1)">18,1)">65); checkBox.CheckState = CheckState.Checked; checkBox.Text = 教育; checkBox = sheet.CheckBoxes.AddCheckBox(); checkBox.Text = 医疗4,1)">IT零售6,1)">其他"; 步骤 5:插入组合框 插入组合框,并指定单元格位置、大小 sheet[A7年龄(段):; sheet[A8<18A918<Y<30A1030<Y<50; IComboBoxShape comboBox = sheet.ComboBoxes.AddComboBox(7,1)">23,1)">100); comboBox.ListFillRange = sheet[A8:A10"]; 步骤 6:指定Combox的关联单元格 sheet[A12代表人群类别:; comboBox.LinkedCell = sheet.Range[B12]; comboBox.SelectedIndex = 1; 步骤 7:保存文档 workbook.SaveToFile(AddFormControls.xlsx",ExcelVersion.Version2010); 运行该项目程序,生成文件(可在项目文件夹bin>Debug下查看文档) 全部代码: using Spire.Xls; Spire.Xls.Core; System.Drawing; namespace FormControls_XLS { class Program { static void Main(string[] args) { 实例化一个Workbook类实例,并获取第1个工作表 Workbook workbook = Workbook(); Worksheet sheet = workbook.Worksheets[]; 设置表格行高、列宽 sheet.Range[ 15F; sheet.Range["].RowHeight = 20F; 插入文本框控件,指定文本框位置、大小以及文本对齐方式 sheet.Range[; ITextBoxShape textBox = sheet.TextBoxes.AddTextBox(); textBox.Text = ; textBox.HAlignment = CommentHAlignType.Center; textBox.VAlignment = CommentVAlignType.Center; 插入单选按钮,指定单元格位置 sheet.Range[; IRadioButton radioButton = sheet.RadioButtons.Add(); radioButton.CheckState = CheckState.Checked; radioButton.Text = ; radioButton = sheet.RadioButtons.Add(); radioButton.Text = ; 插入复选框并指定单元格位置 sheet.Range[; ICheckBox checkBox = sheet.CheckBoxes.AddCheckBox(); checkBox.CheckState = CheckState.Checked; checkBox.Text = ; checkBox = sheet.CheckBoxes.AddCheckBox(); checkBox.Text = ; 插入组合框,并指定单元格位置、大小 sheet[; sheet[; IComboBoxShape comboBox = sheet.ComboBoxes.AddComboBox(); comboBox.ListFillRange = sheet[指定组合框的关联单元格 sheet[; comboBox.LinkedCell = sheet.Range[]; comboBox.SelectedIndex = 1保存文档 workbook.SaveToFile(,ExcelVersion.Version2010); System.Diagnostics.Process.Start(); } } } ? 【示例 2】 删除Excel表单控件?步骤 1:加载文档,并获取指定单元格 Workbook workbook = 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全部代码: |