gpt4 book ai didi

c# - 有没有一种 SAX 方法可以遍历 OpenXML 行?

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:58 28 4
gpt4 key购买 nike

我正在使用提供的 SAX 方法解析一个大文件: Parsing and Reading Large Excel Files with the Open XML SDK

这是我修改后的版本(为简单起见只获取行号)

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 方法可以只循环遍历行而不是工作表中的每个项目?

谢谢,

最佳答案

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

关于c# - 有没有一种 SAX 方法可以遍历 OpenXML 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6589625/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com