0

I am using openxml sdk 2.5 with the SAX approach. What I want to do is iterate through the rows and grab specific cell values by column name.

I have a spreadsheet with only 1 worksheet in it and rows and columns that look like this

    (Rows) ID, Date, type, description, ...
(columns)  4, 07/01/2014, outType, description goes here...

I can't even traverse the rows let alone get the cell values. Not sure how to do this correctly after following some stackoverflow examples. Here is my code.

 WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                    WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
                    OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);
                    String rowNum;
                    while (reader.Read())
                    {
                        if (reader.ElementType == typeof(Row))
                        {
                            do
                            {
                                if (reader.HasAttributes)
                                    rowNum = reader.Attributes.First(a => a.LocalName == "r").Value;

                            } while (reader.ReadNextSibling()); // Skip to the next row
                            break; // We just looped through all the rows so no need to continue reading the worksheet
                        }

                        if (reader.ElementType != typeof(Worksheet)) // Dont' want to skip the contents of the worksheet
                            reader.Skip(); // Skip contents of any node before finding the first row.
                    }

My elementType in this code is of type worksheet and not row, When I call the same code with multiple worksheets it would iterate through the rows.

Train
  • 3,420
  • 2
  • 29
  • 59
  • 1
    `workbookPart.WorksheetParts.First()` doesn't always return the first sheet that Excel displays. Are you sure you're reading the correct sheet? My [answer here](http://stackoverflow.com/questions/29114979/relationship-between-sheet-and-worksheet/29123372#29123372) shows how to find the sheet by name. – petelids Jun 07 '16 at 15:51
  • That was actually the example I found that worked. Funny it was your answer. Thank you. – Train Jun 07 '16 at 16:20

0 Answers0