Other API Samples

Live SpreadsheetGear API Samples

Workbook Miscellaneous Manage Sheets, Worksheets and Chart Sheets

IWorkbook contains several collections to access various sheets in a workbook. IWorkbook.Sheets[...] contains all sheets in a workbook (worksheets, chart sheets as well as lesser-used "Macro Sheet" and "VB Module" sheet types. IWorkbook.Worksheets[...] contains only worksheets; IWorkbook.ChartSheets[...] contains only chart sheets. Also demonstrated is API to move and copy sheets from one location to another.

// Open workbook with 3 worksheets and 2 chart sheets.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(@"WorkbookSheets.xlsx");

// List all sheets in workbook.
foreach (SpreadsheetGear.ISheet sheet in workbook.Sheets)
    System.Console.Write($"{sheet.Name} ");
// OUTPUT: Sheet1 Sheet2 Sheet3 Chart1 Chart2 

// List only worksheets.
foreach (SpreadsheetGear.IWorksheet worksheet in workbook.Worksheets)
    System.Console.WriteLine(worksheet.Name);
// OUTPUT: Sheet1 Sheet2 Sheet3 

// List only chart sheets.
foreach (SpreadsheetGear.IChartSheet chartSheet in workbook.ChartSheets)
    System.Console.WriteLine(chartSheet.Name);
// OUTPUT: Chart1 Chart2 

// Add 2 new sheets and delete one.
SpreadsheetGear.ISheet newSheet1 = workbook.Worksheets.Add();
SpreadsheetGear.ISheet newSheet2 = workbook.Worksheets.Add();
newSheet2.Delete();
Run This Sample
Download File

Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.


Supporting Files

The following files are utilized by this sample: