Excel is a professional document used to edit different data information in most cases. But sometimes, people need to insert some text also to explain, comment, remark or something else to help people understand the data information more clearly. Thus, insert textbox becomes necessary.
In order to realize this task smoothly, I would like to use a .NET component Spire.XLS to help me insert textbox in excel with C#,VB.NET. Spire.XLS uses four steps to finish this task. So it is a good choice to install Spire.XLS in the system.
Preview
You only need the above simple four steps to finish inserting textbox in excel with C#,VB.NET by using Spire.XLS. So why not give it a try? More excel functions realized with C#,VB,.NET can be found below:
More functions
In order to realize this task smoothly, I would like to use a .NET component Spire.XLS to help me insert textbox in excel with C#,VB.NET. Spire.XLS uses four steps to finish this task. So it is a good choice to install Spire.XLS in the system.
How to insert textbox in excel with C#,VB.NET
Before we start, please create a project in Visual Studio and set your target Framework to be .NET Framework 4. Then, add Spire.XLS DLL as the reference. When you add using Spire.XLS at the top of the method, please make sure that you also add using Spire.Xls.Core.
Procedure
Step1. Create a workbook and initialize worksheet.
Inset Textbox with C# Code:
Workbook workbook = new Workbook();
workbook.CreateEmptySheets(1);
Worksheet sheet = workbook.Worksheets[0];
Inset Textbox with VB.NET Code:
Dim workbook As New Workbook()
workbook.CreateEmptySheets(1)
Dim sheet As Worksheet = workbook.Worksheets(0)
Before we start, please create a project in Visual Studio and set your target Framework to be .NET Framework 4. Then, add Spire.XLS DLL as the reference. When you add using Spire.XLS at the top of the method, please make sure that you also add using Spire.Xls.Core.
Procedure
Step1. Create a workbook and initialize worksheet.
Inset Textbox with C# Code:
Workbook workbook = new Workbook();
workbook.CreateEmptySheets(1);
Worksheet sheet = workbook.Worksheets[0];
Inset Textbox with VB.NET Code:
Dim workbook As New Workbook()
workbook.CreateEmptySheets(1)
Dim sheet As Worksheet = workbook.Worksheets(0)
Step2. Insert Texbox in excel.
In this step, I use the “ sheet. TextBoxes. AddTextBox()” method to insert textbox in excel. There are four parameters passed to this method: int row( textbox beginning row), int column(textbox beginning column), textbox height and width.
Inset Textbox with C# Code:
ITextBox textbox = sheet.TextBoxes.AddTextBox(4, 6, 200, 400);
Inset Textbox with VB.NET Code:
Dim textbox As ITextBox = sheet.TextBoxes.AddTextBox(4, 6, 200, 400)
In this step, I use the “ sheet. TextBoxes. AddTextBox()” method to insert textbox in excel. There are four parameters passed to this method: int row( textbox beginning row), int column(textbox beginning column), textbox height and width.
Inset Textbox with C# Code:
ITextBox textbox = sheet.TextBoxes.AddTextBox(4, 6, 200, 400);
Inset Textbox with VB.NET Code:
Dim textbox As ITextBox = sheet.TextBoxes.AddTextBox(4, 6, 200, 400)
Step3. Set texbox format
I set the text rotation to be left to right and set the text alignment. There are two types of alignment: vertical and horizontal. In this method, I set both types as center.
Inset Textbox with C# Code:
textbox.TextRotation = TextRotationType.LeftToRight;
textbox.HAlignment = CommentHAlignType.Center;
textbox.VAlignment = CommentVAlignType.Center;
textbox.Text = "Insert TextBox in Workbook";
Inset Textbox with VB.NET Code:
textbox.TextRotation = TextRotationType.LeftToRight
textbox.HAlignment = CommentHAlignType.Center
textbox.VAlignment = CommentVAlignType.Center
textbox.Text = "Insert TextBox in Workbook"
I set the text rotation to be left to right and set the text alignment. There are two types of alignment: vertical and horizontal. In this method, I set both types as center.
Inset Textbox with C# Code:
textbox.TextRotation = TextRotationType.LeftToRight;
textbox.HAlignment = CommentHAlignType.Center;
textbox.VAlignment = CommentVAlignType.Center;
textbox.Text = "Insert TextBox in Workbook";
Inset Textbox with VB.NET Code:
textbox.TextRotation = TextRotationType.LeftToRight
textbox.HAlignment = CommentHAlignType.Center
textbox.VAlignment = CommentVAlignType.Center
textbox.Text = "Insert TextBox in Workbook"
Step4. Save and launch the file
Inset Textbox with C# Code:
workbook.SaveToFile("ExcelTextBox.xls", ExcelVersion.Version97to2003);
System.Diagnostics.Process.Start("ExcelTextBox.xls");
Inset Textbox with VB.NET Code:
workbook.SaveToFile("ExcelTextBox.xls", ExcelVersion.Version97to2003)
System.Diagnostics.Process.Start("ExcelTextBox.xls")
Preview
You only need the above simple four steps to finish inserting textbox in excel with C#,VB.NET by using Spire.XLS. So why not give it a try? More excel functions realized with C#,VB,.NET can be found below:
More functions
No comments:
Post a Comment