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.