是否有一种SAX方法来遍历OpenXML行

本文关键字:方法 遍历 OpenXML SAX 一种 是否 | 更新日期: 2023-09-27 18:01:21

我正在使用以下提供的SAX方法解析一个大文件:使用Open XML SDK解析和读取大型Excel文件

这是我修改后的版本(为了简单,只得到行号)

using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open("BigFile.xlsx", true))
{
    WorkbookPart workbookPart = myDoc.WorkbookPart;
    WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
    OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);
    String rowNum;
    while (reader.Read())
    {
        if (reader.ElementType == typeof(Row))
        {
            if (reader.HasAttributes)
                rowNum = reader.Attributes.First(a => a.LocalName == "r").Value
        }
    }
}

问题是,它循环遍历每个项/单元格/列/诸如此类的东西,并且只在元素类型为Row时起作用。

是否有SAX方法只通过行而不是工作表中的每个项目进行循环?

谢谢,

是否有一种SAX方法来遍历OpenXML行

关键是要使用阅读器的Skip()ReadNextSibling()方法…

 using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open("BigFile.xlsx", true))
 {
     WorkbookPart workbookPart = myDoc.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.
     }
}