Openxml Excel Processing プロジェクトに取り組んでいます。特定のクラスには、Row オブジェクトがあります。Row オブジェクトのみを使用して、この Row オブジェクトの現在のスプレッドシートを取得できますか?
1 に答える
1
インスタンスのParentプロパティを使用して、行が属する行に移動できます。RowWorksheet
行クラスに関する次のMSDN 記事でRowは、インスタンスの唯一の親要素はSheetData行が属するオブジェクトであると明確に述べています。オブジェクトの可能な唯一の親オブジェクトSheetDataは
Worksheetインスタンスです (詳細については、SheetData クラスに関する次の MSDN 記事を参照してください)。
例:
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...
}
于 2012-07-25T20:45:03.423 に答える