News Category

C#/VB.NET: Add Worksheets to Excel

2022-04-15 08:25:00 Written by  support iceblue
Rate this item
(0 votes)

When working with an existing Excel file or creating an Excel file from scratch, we may need to add one or more worksheets to record data. In this article, we will demonstrate how to add worksheets to Excel in C# and VB.NET using Spire.XLS for .NET library.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Add a Worksheet to an Existing Excel file in C# and VB.NET

The following are the steps to add a worksheet to an existing Excel file:

  • C#
  • VB.NET
using Spire.Xls;

namespace AddWorksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

            //Add a worksheet 
            Worksheet sheet = workbook.Worksheets.Add("New_Sheet");

            //Add data to cell (1, 1)
            sheet.Range[1, 1].Value = "New Sheet";

            //Save the result file
            workbook.SaveToFile("AddWorksheets.xlsx", ExcelVersion.Version2016);
        }
    }
}

C#/VB.NET: Add Worksheets to Excel

Add a Worksheet to a New Excel File in C# and VB.NET

The following steps show how to create a new Excel file and add a worksheet to it:

  • C#
  • VB.NET
using Spire.Xls;

namespace AddWorksheetToNewExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Clear the default worksheets
            workbook.Worksheets.Clear();
            
            //Add a worksheet with name
            Worksheet sheet = workbook.Worksheets.Add("Sheet1");

            //Add data to cell (1, 1)
            sheet.Range[1, 1].Value = "Sheet 1";

            //Save the result file
            workbook.SaveToFile("AddWorksheets.xlsx", ExcelVersion.Version2016);
        }
    }
}

C#/VB.NET: Add Worksheets to Excel

Add Multiple Worksheets to a New Excel File in C# and VB.NET

The following steps show how to create a new Excel file and add 3 worksheets to it:

  • C#
  • VB.NET
using Spire.Xls;

namespace AddWorksheetsToNewExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Add 3 worksheets 
            workbook.CreateEmptySheets(3);

            //Loop through the worksheets
            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                Worksheet sheet = workbook.Worksheets[i];
                //Add data to cell (1, 1) in each worksheet
                sheet.Range[1, 1].Value = "Sheet " + (i + 1);
            }

            //Save the result file
            workbook.SaveToFile("AddWorksheetsToNewExcel.xlsx", ExcelVersion.Version2016);
        }
    }
}

C#/VB.NET: Add Worksheets to Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Friday, 15 April 2022 02:54