open xml sdk 2.5无法遍历行并获取单元格值(Sax方法)

时间:2016-06-07 14:19:07

标签: c# openxml-sdk

我正在使用带有SAX方法的openxml sdk 2.5。我想要做的是遍历行并按列名获取特定的单元格值。

我有一个电子表格,其中只有一个工作表,行和列看起来像这样

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

我甚至无法遍历行,更不用说获取单元格值了。遵循一些stackoverflow示例后,不确定如何正确执行此操作。这是我的代码。

 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.
                    }

此代码中的elementType类型为worksheet而非row,当我使用多个工作表调用相同的代码时,它将遍历行。

0 个答案:

没有答案