1

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?

5
  • You are creating an array(allTheSheets) with 0 length. Have you considered to use List? Commented Oct 20, 2013 at 20:55
  • An array is fixed sized. You cannot resize it. So you have to size it correctly in the first place. If you don't know the size use a List<String> instead. Commented Oct 20, 2013 at 20:56
  • Your code is a bit strange. Do you know on which line you get the exception. Looking carefully you have an index in the array that is always zero and this render your array useless. Commented Oct 20, 2013 at 21:02
  • @L.B YES! That seems to have worked!!! Relatively new here, is there an upvote or something I can give you? Commented Oct 20, 2013 at 21:07
  • @Steve my code is strange because I have almost no idea what I'm doing, lol. Commented Oct 20, 2013 at 21:08

1 Answer 1

3

You have created an array that could contain just one element and this element will be stored at index 0. Of course if you have more than one instance of class A in your inner loop you need more elements in the array.

Instead of using arrays you could change your code to use a List<string>

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
{
    List<string> allTheSheets = new List<string>();
    List<string> allTheData = new List<string>();
    S sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;


    foreach (E sheet in sheets)
    {
        foreach (A attr in sheet.GetAttributes())
        {
            string sheetName = attr.Value;
            allTheSheets.Add(attr.Value.ToString());
        }
    }

At the end of this two loops you will have all the A values in your list allTheSheets and you can use another foreach to look at its content.

Said that, your code looks a bit strange. The index used to store and print the string element is always zero and thus you should not have any Index Out Of Bound.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. Tried the list and it seems to be working just fine! I appreciate the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.