gpt4 book ai didi

c# - 从 Open Xml 中的行获取电子表格

转载 作者:行者123 更新时间:2023-11-30 17:59:05 26 4
gpt4 key购买 nike

我正在处理 Openxml Excel Processing 项目。在某个类中,我有 Row 对象。我可以仅使用 Row 对象获取此 Row 对象的当前电子表格吗?

最佳答案

您可以使用 Row 实例的 Parent 属性来导航到该行所属的 Worksheet

以下MSDN article on the Row class清楚地说,唯一的父元素Row 实例是该行所属的 SheetData 对象。SheetData 对象唯一可能的父对象是Worksheet 实例(请参阅以下 MSDN article on the SheetData class 了解更多信息)。

例子:

以下示例显示如何导航到 Worksheet给定的 Row 实例属于。为了方便我创建了扩展方法:

public static class RowExtensionMethods
{
public static Worksheet GetWorksheet(this Row r)
{
if (r.Parent == null)
{
throw new InvalidOperationException("Row does not belong to sheetdata.");
}

if (r.Parent.Parent == null)
{
throw new InvalidOperationException("Row does not belong to worksheet.");
}

return (Worksheet)r.Parent.Parent;
}
}


// Then in your method use the extension method like so
public void YourMethod(Row r)
{
Worksheet w = r.GetWorksheet();

// Do something with the worksheet...
}

关于c# - 从 Open Xml 中的行获取电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11568915/

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