How to remove worksheet in C#

Spire.XLS supports to add a new worksheet to the existing Excel workbook, it also supports to remove a worksheet from the Excel workbook in C#. This article focus on demonstrating how to remove Excel worksheet using sheet name or sheet index with the help of Spire.XLS.

Firstly, view the sample Excel Workbook with three sheets:

How to remove worksheet in C#

Step 1: Initialize an instance of Workbook and load the document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Remove the worksheet by using its sheet name or sheet index by the method of Worksheets.Remove().

// removing a worksheet using its sheet name
workbook.Worksheets.Remove("Report");

// removing a worksheet using its sheet index  
workbook.Worksheets[0].Remove();

Step 3: Save the document to file.

workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);

Effective screenshot after removing two worksheets from the Excel workbook:

How to remove worksheet in C#

Full codes:

static void Main(string[] args)
{

    Workbook workbook = new Workbook();
    workbook.LoadFromFile("Sample.xlsx");
    
    workbook.Worksheets.Remove("Report");

    workbook.Worksheets[0].Remove();

    workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);
              
}