How to Insert Controls to Worksheet in C#, VB.NET

2015-01-13 08:37:38 Written by  support iceblue
Rate this item
(0 votes)

MS Excel contains a set of Windows Forms controls that can be used to Worksheet to host item. Spire.XLS also provides programmers similar features to add controls on Worksheet at runtime without installing any other control program. In this article, I'll introduce you how to insert TextBox, CheckBox and RadioButton into Worksheet via Spire.XLS in C#, VB.NET.

Detailed Steps:

Step 1: Download Spire.XLS and reference dll file to your VS project.

Step 2: Use Spire.Xls.Core as namespace, which contains all the interfaces like ITextBoxShap, ICheckBox, IRadioButton and etc.

Step 3: Initialize a new instance of Workbook and create a Worksheet in it.

Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];

Step 4: Insert a TextBox at specified location and input the display text.

ITextBoxShape textbox = ws.TextBoxes.AddTextBox(2, 2, 15, 100);
textbox.Text = "Hello World";

Step 5: Insert three CheckBox into Worksheet at different locations. Set the CheckState and display text.

ICheckBox cb = ws.CheckBoxes.AddCheckBox(4, 2, 15, 100);
cb.CheckState = CheckState.Checked;
cb.Text = "Check Box 1";

cb = ws.CheckBoxes.AddCheckBox(4, 4, 15, 100);
cb.CheckState = CheckState.Checked;
cb.Text = "Check Box 2";

cb = ws.CheckBoxes.AddCheckBox(4, 6, 15, 100);
cb.CheckState = CheckState.Checked;
cb.Text = "Check Box 3";

Step 6: Insert three RadioButton and set the related properties.

IRadioButton rb = ws.RadioButtons.Add(6, 2, 15, 100);
rb.Text = "Option 1";

rb = ws.RadioButtons.Add(8, 2, 15, 100);
rb.CheckState = CheckState.Checked;
rb.Text = "Option 2";

rb = ws.RadioButtons.Add(10, 2, 15, 100);
rb.Text = "Option 3";

Step 7: Save the file.

ws.DefaultRowHeight = 15;
wb.SaveToFile("Result.xlsx", ExcelVersion.Version2010);

Output:

How to Insert Controls to Worksheet in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
using Spire.Xls.Core;
namespace InsertControl
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook wb = new Workbook();
            Worksheet ws = wb.Worksheets[0];

            ITextBoxShape textbox = ws.TextBoxes.AddTextBox(2, 2, 15, 100);
            textbox.Text = "Hello World";

            ICheckBox cb = ws.CheckBoxes.AddCheckBox(4, 2, 15, 100);
            cb.CheckState = CheckState.Checked;
            cb.Text = "Check Box 1";

            cb = ws.CheckBoxes.AddCheckBox(4, 4, 15, 100);
            cb.CheckState = CheckState.Checked;
            cb.Text = "Check Box 2";

            cb = ws.CheckBoxes.AddCheckBox(4, 6, 15, 100);
            cb.CheckState = CheckState.Checked;
            cb.Text = "Check Box 3";

            IRadioButton rb = ws.RadioButtons.Add(6, 2, 15, 100);
            rb.Text = "Option 1";

            rb = ws.RadioButtons.Add(8, 2, 15, 100);
            rb.CheckState = CheckState.Checked;
            rb.Text = "Option 2";

            rb = ws.RadioButtons.Add(10, 2, 15, 100);
            rb.Text = "Option 3";

            ws.DefaultRowHeight = 15;
            wb.SaveToFile("Result.xlsx", ExcelVersion.Version2010);
        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports Spire.Xls.Core
Namespace InsertControl
	Class Program
		Private Shared Sub Main(args As String())
			Dim wb As New Workbook()
			Dim ws As Worksheet = wb.Worksheets(0)

			Dim textbox As ITextBoxShape = ws.TextBoxes.AddTextBox(2, 2, 15, 100)
			textbox.Text = "Hello World"

			Dim cb As ICheckBox = ws.CheckBoxes.AddCheckBox(4, 2, 15, 100)
			cb.CheckState = CheckState.Checked
			cb.Text = "Check Box 1"

			cb = ws.CheckBoxes.AddCheckBox(4, 4, 15, 100)
			cb.CheckState = CheckState.Checked
			cb.Text = "Check Box 2"

			cb = ws.CheckBoxes.AddCheckBox(4, 6, 15, 100)
			cb.CheckState = CheckState.Checked
			cb.Text = "Check Box 3"

			Dim rb As IRadioButton = ws.RadioButtons.Add(6, 2, 15, 100)
			rb.Text = "Option 1"

			rb = ws.RadioButtons.Add(8, 2, 15, 100)
			rb.CheckState = CheckState.Checked
			rb.Text = "Option 2"

			rb = ws.RadioButtons.Add(10, 2, 15, 100)
			rb.Text = "Option 3"

			ws.DefaultRowHeight = 15
			wb.SaveToFile("Result.xlsx", ExcelVersion.Version2010)
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Insert Controls to Worksheet
Last modified on Monday, 06 September 2021 02:32