是否有 SAX 方式来循环 OpenXML 行?

发布于 2024-11-18 18:10:05 字数 914 浏览 2 评论 0 原文

我正在使用以下提供的 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
        }
    }
}

问题是,这会循环遍历每个item/cell/column/whatnot 仅当元素类型为 Row 时才起作用。

是否有一种 SAX 方法可以仅循环遍历行而不是工作表中的每个项目?

谢谢,

I'm parsing a large file using the SAX approach offered on:
Parsing and Reading Large Excel Files with the Open XML SDK

This is my modified version (only getting the row number for simplicity)

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

The problem is that this loops through every item/cell/column/whatnot and only acts when the element type is Row.

Is there a SAX way to loop only through the rows and not every item in the worksheet?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深者入戏 2024-11-25 18:10:05

关键是使用阅读器的 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.
     }
}

The key is to use the Skip() and ReadNextSibling() methods of the reader...

 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.
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文