Class WorksheetsCollection
Class for managing the collection of WorkSheet elements.
Inheritance
Implements
Namespace: IronXL
Assembly: IronXL.dll
Syntax
public class WorksheetsCollection : Object
When code needs to walk, add, reorder, or remove the sheets of an Excel file in C#, WorksheetsCollection is the type it works through. It is the list of worksheets a WorkBook exposes as its WorkSheets property, so iterating every tab, counting them, or inserting a new one all run against this collection rather than the workbook directly.
A workbook hands back its collection through WorkSheets, and from there the surface is a familiar list. Index a sheet by position with Item[Int32], so workBook.WorkSheets[0] is the first tab, and read how many there are with Count. The collection implements IList<WorkSheet>, so it supports GetEnumerator for foreach iteration over each WorkSheet.
The members fall into a few jobs. For adding, Create builds a new named sheet in place and Add appends an existing WorkSheet, while Insert places one at a chosen index. For removal, Remove takes a sheet instance, RemoveAt takes a position, and Clear empties the collection. For ordering and lookup, SetSheetIndex moves a sheet to a new position, IndexOf and FindIndex locate one, and Contains tests membership. Changes to the collection are part of the workbook, so they persist when the parent WorkBook is saved. To create or fetch a sheet by name rather than by position, the workbook's own CreateWorkSheet and GetWorkSheet are the more direct route.
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
foreach (WorkSheet sheet in workBook.WorkSheets)
Console.WriteLine(sheet.Name);
WorkSheet added = workBook.WorkSheets.Create("Summary");
workBook.SaveAs("output.xlsx");The manage worksheets how-to walks through adding and removing sheets, the Excel worksheets example iterates a collection, and the copy worksheet how-to moves sheets between workbooks.
Properties
Count
Gets the number of elements contained in the collection.
Declaration
public int Count { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | The count of worksheets. |
IsReadOnly
Gets a value indicating whether the collection is read-only.
Declaration
public bool IsReadOnly { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
Item[Int32]
Gets or sets the WorkSheet at the specified index.
Declaration
public WorkSheet this[int index] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The index. |
Property Value
| Type | Description |
|---|---|
| WorkSheet | The WorkSheet. |
Methods
Add(WorkSheet)
Adds an object to the end of the collection.
Declaration
public void Add(WorkSheet item)
Parameters
| Type | Name | Description |
|---|---|---|
| WorkSheet | item | The object to be added to the end of the collection. |
Clear()
Delete all worksheets in the list.
Declaration
public void Clear()
Contains(WorkSheet)
Determines whether worksheets collection contains the element.
Declaration
public bool Contains(WorkSheet item)
Parameters
| Type | Name | Description |
|---|---|---|
| WorkSheet | item | The element to find. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
CopyTo(WorkSheet[], Int32)
Copies the entire collection to a compatible one-dimensional array, starting at the specified index of the target array.
Declaration
public void CopyTo(WorkSheet[] array, int arrayIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| WorkSheet[] | array | The destination of the elements copied from collection. The array must have zero-based indexing. |
| System.Int32 | arrayIndex | The zero-based index in |
Create(String)
Create new worksheet with the specified name.
Declaration
public WorkSheet Create(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | name | The name of the new worksheet. |
Returns
| Type | Description |
|---|---|
| WorkSheet | A new WorkSheet within the workbook. |
FindIndex(Predicate<WorkSheet>)
Searches for an element that matches the conditions defined by the specified predicate.
Declaration
public int FindIndex(Predicate<WorkSheet> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<WorkSheet> | predicate | The predicate delegate that defines the conditions of the element to search for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The zero-based index of the first occurrence of an element that matches
the conditions defined by |
GetEnumerator()
Returns an enumerator that iterates through the collection.
Declaration
public IEnumerator<WorkSheet> GetEnumerator()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerator<WorkSheet> | An enumerator that can be used to iterate through the collection. |
IndexOf(WorkSheet)
Searches for the specified object and returns the zero-based index of the first occurrence within the entire collection.
Declaration
public int IndexOf(WorkSheet item)
Parameters
| Type | Name | Description |
|---|---|---|
| WorkSheet | item | The object to locate in the collection. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The zero-based index of the first occurrence of |
Insert(Int32, WorkSheet)
Inserts an element into the collection at the specified index.
Declaration
public void Insert(int index, WorkSheet item)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The zero-based index at which |
| WorkSheet | item | The object to insert. |
Remove(WorkSheet)
Removes the specified WorkSheet item.
Declaration
public bool Remove(WorkSheet item)
Parameters
| Type | Name | Description |
|---|---|---|
| WorkSheet | item | The item to remove. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
RemoveAt(Int32)
Removes the element at the specified index of collection.
Declaration
public void RemoveAt(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The zero-based index of the element to remove. |
SetSheetIndex(WorkSheet, Int32)
Sets the order of appearance for a given sheet.
Declaration
public void SetSheetIndex(WorkSheet sheet, int index)
Parameters
| Type | Name | Description |
|---|---|---|
| WorkSheet | sheet | The sheet to reorder. |
| System.Int32 | index | The index to insert into. |