Search code examples
c#excelvisual-studiospreadsheetlight

How to loop through unknown worksheet names in a workbook using the SpreadsheetLight Library


I'm trying to loop through unknown worksheet names in a workbook using the SpreadsheetLight Library.

Using the SLDocument sl = new SLDocument("ModifyExistingSpreadsheetOriginal.xlsx", "Sheet1") statement in the examples only allows a string input for the worksheet.

Is there some way to refer to a worksheet index or do something similar to a foreach string sh in Workbook.Sheets loop?


Solution

  • There's a GetWorksheetNames() method that looks like it will do exactly what you want:

    var sl = new SLDocument("ModifyExistingSpreadsheetOriginal.xlsx");
    
    foreach (var name in sl.GetWorksheetNames())
    {
        // do something with each worksheet name
    }
    

    From their docs:

    Get a list of names of existing worksheets currently in the spreadsheet, excluding chart sheets, macro sheets and dialog sheets.