I slightly altered some code from msdn.com. I'm trying to get a string array of all the sheet names in an Excel spreadsheet. Can I add some code to the foreach statement so that each time it loops, it places attr.Value into the array?
public static void GetSpreadsheetData(string fileName)
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
{
string[] allTheSheets = new string[0];
string[] allTheData = new string[0];
S sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;
foreach (E sheet in sheets)
{
foreach (A attr in sheet.GetAttributes())
{
int i = 0;
string sheetName = attr.Value;
allTheSheets[i] = attr.Value.ToString();
i++;
Console.WriteLine(allTheSheets[0]);
Console.ReadLine();
}
}
}
}
Here is the error message I'm getting:
"Index was outside the bounds of the array."
One of the things that has me confused is that when I instantiated the array, I gave it an index of [0], so how is that outside the bounds?
0length. Have you considered to use List?List<String>instead.