How C # Adds and removes form controls to excel

Source: Internet
Author: User

In Excel, the added control can be associated with a cell, and we can manipulate the control to modify the contents of the cell, and in the following article we will describe how to add several different form controls in Excel, including:

    • Add a text box
    • radio button
    • check box
    • Combo box

using the tool : Free Spire.xls for. NET 8.3 (Community edition)
PS: After downloading and installing the component, note that the reference Spire.XLS.dll (DLL file can be obtained in the Bin folder under the installation path) in the project program, as shown in

code example:

    1. Insert an Excel form control
      "C #"
Using spire.xls;using spire.xls.core;using System.drawing;namespace formcontrols_xls{class Program {static void Main (string[] args) {//Instantiate a Workbook class instance and add a worksheet Workbook Workbook = new Wor to the workbook            Kbook (); Worksheet sheet = workbook.            Worksheets[0]; Insert a text box control, set the text box position, size, color, and alignment itextboxshape TextBox = sheet.            Textboxes.addtextbox (1, 1, 19, 65);            TextBox.Fill.ForeColor = Color.green; Sets the table row height, column width sheet. range["A2:a3"].            ColumnWidth = 30F; Sheet. range["A2:b11"].           RowHeight = 20F;            Add text box, set alignment TextBox.Text = "Mobile phone consumption questionnaire survey";            Textbox.halignment = Commenthaligntype.center;            Textbox.valignment = Commentvaligntype.center; Inserts a radio button and assigns a sheet to the cell location. range["A3"].            Text = "Your Gender:"; Iradiobutton RadioButton = sheet.            Radiobuttons.add (3, 2, 20, 80);            Radiobutton.checkstate = checkstate.checked; RadiobUtton.            Text = "female"; RadioButton = sheet.            Radiobuttons.add (3, 3, 20, 80);            Radiobutton.text = "male"; Inserts a check box and specifies the cell location sheet. range["A5"].            Text = "Your industry:"; Icheckbox CheckBox = sheet.            Checkboxes.addcheckbox (5, 2, 18, 65);            Checkbox.checkstate = checkstate.checked;            Checkbox.text = "Education"; CheckBox = sheet.            Checkboxes.addcheckbox (5, 3, 18, 65);            Checkbox.text = "medical"; CheckBox = sheet.            Checkboxes.addcheckbox (5, 4, 18, 65);            Checkbox.text = "IT"; CheckBox = sheet.            Checkboxes.addcheckbox (5, 5, 18, 65);            Checkbox.text = "Retail"; CheckBox = sheet.            Checkboxes.addcheckbox (5, 6, 18, 65);            Checkbox.text = "other"; Sheet. range["A7"].            Text = "Your phone's appearance requires:"; CheckBox = sheet.            Checkboxes.addcheckbox (7, 2, 18, 65);            Checkbox.checkstate = checkstate.checked;            Checkbox.text = "screen Size"; CheckBox = sheet. Checkboxes.addcheckboX (7, 3, 18, 65);            Checkbox.text = "exterior material"; CheckBox = sheet.            Checkboxes.addcheckbox (7, 4, 18, 65);            Checkbox.checkstate = checkstate.checked;            Checkbox.text = "appearance color"; Insert a combo box, specify a position and set the associated cell sheet["A9"].            Text = "Your phone's functional requirements:"; sheet["A10"].            Text = "Entertainment"; sheet["A11"].            Text = "Business"; sheet["A12"].            Text = "other";            Icomboboxshape ComboBox = Sheet.ComboBoxes.AddComboBox (9, 2, 18, 65);            Combobox.listfillrange = sheet["A10:a12"]; Combobox.linkedcell = sheet.            range["C9"];            Combobox.selectedindex = 2; Save and open the document workbook.            SaveToFile ("Addformcontrols.xlsx", excelversion.version2010);        System.Diagnostics.Process.Start ("addformcontrols.xlsx"); }    }}

Run the project program, generate the file (you can view the document under Bin>debug under Project folder)

    1. Delete an Excel form control
      "C #"
using Spire.Xls;namespace RemoveFormControl_XLS{    class Program    {        static void Main(string[] args)        {            //创建Workbook实例,加载Excel文档            Workbook workbook = new Workbook();            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);            System.Diagnostics.Process.Start("RemoveCheckBoxes.xlsx");        }    }}

Form Delete Effect:

This is all about C # operations for Excel form controls.

End of this article

(If you need to reprint, please specify the source)

How C # Adds and removes form controls to excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.