Add Excel Worksheet in C#, VB.NET

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.

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

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

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

Adding multiple Excel worksheets to Excel file:

Add Excel Worksheet