How to Manage Worksheet

by Chaknith Bin

IronXL library makes managing worksheet using C# code as easy as possible. The actions of create & delete worksheet, change worksheets position, and set active worksheet in Excel file can be achieved without using Office Interop.

C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
C# Excel DLL

Download DLL

Download DLL

Manually install into your project

Manage Worksheet Example

Managing Worksheet requires the ability to create, move, and delete worksheet. IronXL can achieve each of these actions with a single line of code.

Please note

  • All the index position mentioned below follows zero-based indexing.

Create Worksheet

The CreateWorkSheet method allows creating worksheet possible. It requires the worksheet name as the only parameter. This method will also return the created worksheet object back, therefore more process such as merge cells can be done right away after creating it.

using IronXL;

// Create new Excel spreadsheet
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);

// Create worksheets
WorkSheet workSheet1 = workBook.CreateWorkSheet("workSheet1");
WorkSheet workSheet2 = workBook.CreateWorkSheet("workSheet2");
WorkSheet workSheet3 = workBook.CreateWorkSheet("workSheet3");
WorkSheet workSheet4 = workBook.CreateWorkSheet("workSheet4");

Imports IronXL

' Create new Excel spreadsheet
Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)

' Create worksheets
Private workSheet1 As WorkSheet = workBook.CreateWorkSheet("workSheet1")
Private workSheet2 As WorkSheet = workBook.CreateWorkSheet("workSheet2")
Private workSheet3 As WorkSheet = workBook.CreateWorkSheet("workSheet3")
Private workSheet4 As WorkSheet = workBook.CreateWorkSheet("workSheet4")

VB   C#
Create Worksheets

Set Worksheet Position

SetSheetPosition method can be used to change or move worksheet position. The two parameters are required. The worksheet name as String and its index position as Integer.

using IronXL;

WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx");

// Set worksheet position
workBook.SetSheetPosition("workSheet2", 0);

Imports IronXL

Private workBook As WorkBook = WorkBook.Load("createNewWorkSheets.xlsx")

' Set worksheet position
workBook.SetSheetPosition("workSheet2", 0)

VB   C#
Change Worksheet Position

Set Active Worksheet

Set active worksheet means to set which worksheet to be opened by default when the workbook is first opened by other data visualization tool like Excel. To achieve this use SetActiveTab method with the index position of the worksheet.

using IronXL;

WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx");

// Set active for workSheet3

Imports IronXL

Private workBook As WorkBook = WorkBook.Load("createNewWorkSheets.xlsx")

' Set active for workSheet3

VB   C#
Set Active Worksheet

Remove Worksheet

Removing the worksheet can also be done with IronXL. Use RemoveWorkSheet method along with index position of the worksheet. In the case of worksheet's position is unknown, the name of the worksheet can also be used to delete the worksheet.

using IronXL;

WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx");

// Remove workSheet1

// Remove workSheet2

Imports IronXL

Private workBook As WorkBook = WorkBook.Load("createNewWorkSheets.xlsx")

' Remove workSheet1

' Remove workSheet2

VB   C#
Remove Worksheet

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.