範囲から値を取得する
最終更新日: 2010年8月2日
適用対象: SharePoint Server 2010
Excel Web Services では、Excel ブックから値を取得するために、GetCell、GetCellA1、GetRange、および GetRangeA1 の 4 つのメソッドが公開されています。
GetCell メソッドと GetCellA1 メソッドは 1 つのセルの値を返します。たとえば "B1:E2" のような範囲の参照、1 つのセルより大きい名前付き範囲を渡すなど、1 つより多いセルを要求しようとすると、メソッドの呼び出しは失敗します。セルの範囲から値を取得する必要がある場合は、代わりに GetRange メソッドと GetRangeA1 メソッドを使用します。
A1 サフィックスが付いているメソッド (GetCellA1 と GetRangeA1) は、このサフィックスが付いていないメソッド (GetCell と GetRange) とは異なる座標系を使用します。範囲の参照 (H8, A3:D5、Sheet2!A12:G18 など) のような Excel スタイルのセル参照や名前付きの範囲を使用する場合は、A1 サフィックスが付いたメソッドを使用する必要があります。これらのメソッドでは、シートの名前や必要な範囲のアドレスを渡すことができます。ほとんどの場合、抽出のためには、Excel スタイルの参照ではなく、名前付きの範囲を使用する方法が適切です。
数値による座標系を使って Excel の範囲にアクセスする場合は、A1 サフィックスが付かないメソッドを使用する必要があります。一連のセルをループで繰り返すコードを使用する場合、またはアルゴリズムの一部として範囲の座標が動的に計算される場合は、範囲の座標を使用する方が簡単です。
セルの行および列の座標は 0 から始まります。このため、下の例のように "0,0" を指定すると、セル A1 が返されます。
// Call the GetCell method to retrieve a value from a cell.
// The cell is in the first row and first column; that is, cell A1
object[] rangeResult2 = xlservice.GetCell(sessionId, sheetName, 0, 0, true, out outStatus);
' Call the GetCell method to retrieve a value from a cell.
' The cell is in the first row and first column; that is, cell A1
Dim rangeResult2() As Object = xlservice.GetCell(sessionId, sheetName, 0, 0, True, outStatus)
複数の隣接するセルから値を取得する場合は、GetCell メソッドの呼び出しを複数回行う代わりに、GetRange メソッドを使用することを検討してください。これによりサーバーへの往復回数は、複数回ではなく 1 回になります。したがって、場合によっては、GetCell メソッドの代わりに GetRange メソッドを使用することで、明らかなパフォーマンスの向上が得られる可能性があります。
GetRange メソッドや GetRangeA1 メソッドを使用してセルの範囲を取得するときには、オブジェクト配列 (C# では object[]、Visual Basic .NET では Object ()) が返されます。オブジェクト配列は実際にはジャグ配列です。返される配列内の各エントリは、セルを表すオブジェクトの別の配列になります。ジャグ配列の詳細については、「Jagged Arrays (C# Programming Guide) (英語)」(https://msdn.microsoft.com/ja-jp/library/2s05feca.aspx) (英語) を参照してください。
GetCell メソッドと GetRange メソッドを使用して値を取得するには
数値の範囲の座標を使い、開いたブックのセルから値を取得する場合は、GetCell メソッドを使用します。たとえば、以下のようにします。
// Instantiate the Web service and make a status array object. ExcelService xlservice = new ExcelService(); Status[] outStatus; string sheetName = "Sheet2"; // Set the path to a workbook. // The workbook must be in a trusted location. string targetWorkbookPath = "http://myserver02/example/Shared%20Documents/Book1.xlsx"; // Set credentials for requests. xlservice.Credentials = System.Net.CredentialCache.DefaultCredentials; // Call the open workbook, and point to the trusted // location of the workbook to open. string sessionId = xlservice.OpenWorkbook(targetWorkbookPath, "en-US", "en-US", out outStatus); // Call the GetCell method to retrieve a value from a cell. // The cell is in the first row and ninth column. object[] rangeResult2 = xlservice.GetCell(sessionId, sheetName, 0, 8, false, out outStatus);
' Instantiate the Web service and make a status array object. Dim xlservice As New ExcelService() Dim outStatus() As Status Dim sheetName As String = "Sheet2" ' Set the path to a workbook. ' The workbook must be in a trusted location. Dim targetWorkbookPath As String = "http://myserver02/example/Shared%20Documents/Book1.xlsx" ' Set credentials for requests. xlservice.Credentials = System.Net.CredentialCache.DefaultCredentials ' Call the open workbook, and point to the trusted ' location of the workbook to open. Dim sessionId As String = xlservice.OpenWorkbook(targetWorkbookPath, "en-US", "en-US", outStatus) ' Call the GetCell method to retrieve a value from a cell. ' The cell is in the first row and ninth column. Dim rangeResult2() As Object = xlservice.GetCell(sessionId, sheetName, 0, 8, False, outStatus)
数値の範囲の座標を使い、開いたブックの特定の範囲から値を取得する場合は、GetRange メソッドを使用します。
// Instantiate the Web service and make a status array object. ExcelService xlservice = new ExcelService(); Status[] outStatus; RangeCoordinates rangeCoordinates = new RangeCoordinates(); string sheetName = "Sheet1"; ... // Prepare object to define range coordinates // and call the GetRange method. // startCol, startRow, startHeight, and startWidth // get their value from user input. rangeCoordinates.Column = (int)startCol.Value; rangeCoordinates.Row = (int)startRow.Value; rangeCoordinates.Height = (int)startHeight.Value; rangeCoordinates.Width = (int)startWidth.Value; ... object[] rangeResult1s = xlservice.GetRange(sessionId, sheetName, rangeCoordinates, false, out outStatus); foreach (object[] x in rangeResult1s) { foreach (object y in x) { Console.WriteLine(String.Format("{0}", y)); } }
' Instantiate the Web service and make a status array object. Dim xlservice As New ExcelService() Dim outStatus() As Status Dim rangeCoordinates As New RangeCoordinates() Dim sheetName As String = "Sheet1" ... ' Prepare object to define range coordinates ' and call the GetRange method. ' startCol, startRow, startHeight, and startWidth ' get their value from user input. rangeCoordinates.Column = CInt(Fix(startCol.Value)) rangeCoordinates.Row = CInt(Fix(startRow.Value)) rangeCoordinates.Height = CInt(Fix(startHeight.Value)) rangeCoordinates.Width = CInt(Fix(startWidth.Value)) ... Dim rangeResult1s() As Object = xlservice.GetRange(sessionId, sheetName, rangeCoordinates, False, outStatus) For Each x As Object() In rangeResult1s For Each y As Object In x Console.WriteLine(String.Format("{0}", y)) Next y Next x
GetCellA1 メソッドと GetRangeA1 メソッドを使用して値を取得するには
Excel の "A1" という範囲の指定を使い、開いたブックのセルから値を取得する場合は、GetCellA1 メソッドを使用します。たとえば、以下のようにします。
// Instantiate the Web service and make a status array object. ExcelService xlservice = new ExcelService(); Status[] outStatus; string sheetName = "Sheet2"; object[] rangeResult = xlservice.GetCellA1(sessionId, sheetName, "MonthlyPayment", true, out outStatus);
' Instantiate the Web service and make a status array object. Dim xlservice As New ExcelService() Dim outStatus() As Status Dim sheetName As String = "Sheet2" Dim rangeResult() As Object = xlservice.GetCellA1(sessionId, sheetName, "MonthlyPayment", True, outStatus)
Excel の "A1" という範囲の指定を使い、開いたブックの特定の範囲から値を取得する場合は、GetRangeA1 メソッドを使用します。次のコード例では、2 x 3 の範囲、つまり 2 行掛ける 3 列を要求します。コードでは次に、返された各行をループ処理し、各行に含まれる 3 つのセルを取得します。つまり、最初のサイクルでは次のようになります。
rangeResult [0] はセル B2 の値を返します。
rangeResult [1] はセル C2 の値を返します。
rangeResult [2] はセル D2 の値を返します。
2 回目のサイクルでは次のようになります。
rangeResult [0] はセル B3 の値を返します。
rangeResult [1] はセル C3 の値を返します。
rangeResult [2] はセル D3 の値を返します。
object[] rangeResults = xlservice.GetRangeA1(sessionId, "Sheet1", "B2:D3", true, out outStatus); foreach (object[] rangeResult in rangeResults) { Console.WriteLine(String.Format("{0} | {1} | {2}", rangeResult[0], rangeResult[1], rangeResult[2])); }
Dim rangeResults() As Object = xlservice.GetRangeA1(sessionId, "Sheet1", "B2:D3", True, outStatus) For Each rangeResult As Object() In rangeResults Console.WriteLine(String.Format("{0} | {1} | {2}", rangeResult(0), rangeResult(1), rangeResult(2))) Next rangeResult
関連項目
タスク
[ウォークスルー] Excel Web Services を使用してカスタム アプリケーションを開発する