C#: Move or Delete Worksheets in Excel

When working with Excel files that contain multiple worksheets, you may sometimes find that some of the worksheets are no longer necessary or need to be organized in a different way. In these cases, you have the option of moving or deleting the worksheets to better manage your spreadsheet. In this article, you will learn how to move or delete worksheets in Excel in C# using Spire.XLS for .NET.

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 DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Move a Worksheet in Excel in C#

Spire.XLS for .NET provides the Worksheet.MoveWorksheet(int destIndex) method to move an Excel worksheet to a desired position by specifying the index. The following are the detailed steps:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheet[] property.
  • Move the worksheet to another position using Worksheet.MoveWorksheet() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;

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

            //Load an Excel file
            workbook.LoadFromFile("Budget.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Move the worksheet to the 3rd position
            sheet.MoveWorksheet(2);

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

C#: Move or Delete Worksheets in Excel

Delete a Worksheet in Excel in C#

You can remove a specific worksheet from Excel by sheet index or name using the Workbook.Worksheets.RemoveAt(int index) or Workbook.Worksheets.Remove(string sheetName) method. The following are the detailed steps:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Delete a specific worksheet from the file using Workbook.Worksheets.RemoveAt() or Workbook.Worksheets.Remove() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;

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

            //Load an Excel file
            workbook.LoadFromFile("Budget.xlsx");

            //Remove a specific worksheet by index
            workbook.Worksheets.RemoveAt(0);
            //Remove a specific worksheet by name
            //workbook.Worksheets.Remove("Summary");

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

        }
    }
}

C#: Move or Delete Worksheets in 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.