Tuesday, 12 June 2012 08:25

Add Excel Worksheet in C#/VB.NET

Written by  support iceblue
Rate this item
(0 votes)
It is as easy as ABC to add Excel worksheet in C#/VB.NET. In this section, we will demonstrate multiple choices to easily add Excel worksheet in C#/VB.NET by using Spire.Xls for .NET

Spire.Xls for .NET provides a class-Workbook that represents an Excel file. Workbook class contains an Excel worksheet collection that allows accessing each worksheet in the Excel workbook. A worksheet is represented by the Worksheet class. Worksheet class provides a wide range of properties and methods to manage a worksheet.

Spire.Xls for .NET is always welcome to any kind of trial and evaluation. So now please feel free to download Spire.Xls for .NET and then follow our guide to add Excel worksheet in C#/VB.NET or try other function of Spire.Xls for .NET.

Before we code to add Excel worksheet in C#/VB.NET, please add spire.xls dll as reference by Click Project ->Add Reference-> Browse-> Choose the folder contains Spire.XLS for .NET->Bin->.NET 4.0->Spire.XLS.dll. Next we will demonstrate the following choices for adding Excel worksheet.

Step 1 Add worksheets to a new Excel file

In this choice, we will create a new workbook and then add Excel worksheet to it. Please check the code examples below.
[C#]
using Spire.Xls;
namespace add_worksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();            
            workbook.Worksheets.Add("Sample of New Excel");
            workbook.SaveToFile("Sample.xls");
            System.Diagnostics.Process.Start("Sample.xls");
        }
    }
}
[VB.NET]
Imports Spire.Xls
Namespace add_worksheet
	Class Program
		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.Worksheets.Add("Sample of New Excel")
			workbook.SaveToFile("Sample.xls")
			System.Diagnostics.Process.Start("Sample.xls")
		End Sub
	End Class
End Namespace
          
Effective screenshot for adding Excel worksheet to new Excel file:

Add Excel Worksheet

Step 2 Add worksheet to designer spreadsheets

The process of adding worksheets to a designer spreadsheet is entirely the same as that of the above approach except that the Excel file is already created and we need to load that Excel file first before adding worksheet to it. A designer spreadsheet can be loaded with Spire.Xls for .NET. Please check the following code examples.

[C#]
using Spire.Xls;
namespace add_worksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\test.xls");
            workbook.Worksheets.Add("Sample of Designer Excel");
            workbook.SaveToFile("Sample.xls");
            System.Diagnostics.Process.Start("Sample.xls");
        }
    }
}
[VB.NET]
Imports Spire.Xls
Namespace add_worksheet
	Class Program
		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.LoadFromFile("..\test.xls")
			workbook.Worksheets.Add("Sample of Designer Excel")
			workbook.SaveToFile("Sample.xls")
			System.Diagnostics.Process.Start("Sample.xls")
		End Sub
	End Class
End Namespace
          
Effective screenshot for adding Excel worksheet to designer Excel file:

Add Excel Worksheet

Step 3 Create multiple worksheet at one time

Spire.Xls for .NET also holds the ability to create multiple Excel worksheet at one time by calling the method of CreateEmptySheets(int sheetCount). Please check the code samples for that.

[C#]
using Spire.Xls;
namespace add_worksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.CreateEmptySheets(6);
            workbook.SaveToFile("Sample.xls");
            System.Diagnostics.Process.Start("Sample.xls");
        }
    }
}
[VB.NET]
Imports Spire.Xls
Namespace add_worksheet
	Class Program
		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.CreateEmptySheets(6)
			workbook.SaveToFile("Sample.xls")
			System.Diagnostics.Process.Start("Sample.xls")
		End Sub
	End Class
End Namespace
          
Effective screenshot for adding multiple Excel worksheets to Excel file

Add Excel Worksheet



About Spire.XLS
Spire.XLS is now a family with Spire.Xls for WPF, Spire.Xls for Silverlight and Spire.Xls for .NET. All of them enable developers/programmers to manage files within their applications. Learn more about them, please click on the links listed below.
Download Spire.XLS
Spire.XLS family is now in one pack. The Spire.XLS Pack is a compilation of Spire.XLS for .NET, Spire.XLS for Silverlight and Spire.XLS for WPF. Just one click to download all of them and have a free trial.


Additional Info

  • tutorial_title: Add Excel Worksheet
Last modified on Tuesday, 07 January 2014 09:47