ClosedXML cycles through worksheets.

In my book there are 4 sheets with different tab names. Let's say if they are named like this: First, Second, Third, Fourth.

I could not find on the Internet how to iterate over each sheet using a for for loop. When I repeat, I would also like to fix the text on the "Worksheet" tab ("First, Second, etc.").

+6
source share
1 answer

You can grab sheets by name or id, for example:

int index = 1; // note indexes are 1 based in ClosedXML var worksheet = workbook.Worksheet(index); string name = "First"; var worksheet = workbook.Worksheet(name); 

Please note that you only want to do this when you know the sheet name and max id (example)

or you can iterate over the collection of worksheets in a book as such:

 foreach (IXLWorksheet worksheet in workbook.Worksheets) { Console.WriteLine(worksheet.Name); // outputs the current worksheet name. // do the thing you want to do on each individual worksheet. } 

You can find this information in the visual studio by pressing F12 on your book object, you will see all public methods / variables to which you also provide access. IXLWorksheet and IXLWorksheets are what you are looking for.

+6
source

Source: https://habr.com/ru/post/985796/


All Articles