在Excel中,添加的控件能夠和單元格關聯,咱們能夠操做控件來修改單元格的內容,在下面的文章中,將介紹在Excel中添加幾種不一樣的表單控件的方法,包括:html
PS:下載安裝該組件後,注意在項目程序中添加引用Spire.Xls.dll(dll文件可在安裝路徑下的Bin文件夾中獲取),以下圖所示ide
步驟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:插入文本框spa
//插入文本框控件,指定文本框位置、大小以及文本對齊方式 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:插入單選按鈕code
//插入單選按鈕,指定單元格位置 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:插入複選框orm
//插入複選框並指定單元格位置 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:插入組合框htm
//插入組合框,並指定單元格位置、大小 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的關聯單元格blog
sheet["A12"].Text = "表明人羣類別:"; comboBox.LinkedCell = sheet.Range["B12"]; comboBox.SelectedIndex = 1;
步驟 7:保存文檔文檔
workbook.SaveToFile("AddFormControls.xlsx", ExcelVersion.Version2010);
運行該項目程序,生成文件(可在項目文件夾bin>Debug下查看文檔)get
所有代碼:
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"); } } }
步驟 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表單控件的所有內容,本文完。
(如需轉載,請註明出處)