手順 3: Web サービスへのアクセス

プロジェクトに Excel Web Services への参照を追加したところで、次の手順では、Web サービスのプロキシ クラスのインスタンスを作成します。 こうすると、プロキシ クラスのメソッドを呼び出すことによって、Web サービスのメソッドにアクセスできます。 アプリケーションがこれらのメソッドを呼び出すと、Visual Studio によって生成されたプロキシ クラスのコードが、アプリケーションと Web サービスの間の通信を処理します。

最初に、Web サービスのプロキシ クラス ExcelWebService のインスタンスを作成します。 次に、プロキシ クラスを使用して、いくつかの Web サービスのメソッドとプロパティを呼び出します。 呼び出しを使用して、ブックを開き、セッション ID を取得し、既定の資格情報を渡し、範囲座標オブジェクトを定義し、範囲座標オブジェクトを使用する範囲を取得し、ブックを閉じ、SOAP 例外をキャッチします。

Web サービスにアクセスする

ディレクティブを追加するには

  1. 先ほど Web 参照を追加すると、yourProject という <名前の名前空間に ExcelService という名前のオブジェクトが作成されました>。<webReferenceName>。 この例では、オブジェクトの名前は SampleApplication.ExcelWebService です。 このチュートリアルでは、SOAP 例外をキャッチする方法も示します。 これを行うには、System.Web.Services.Protocols オブジェクトを使用します。 名前空間 System.Web.Services.Protocols は、XML Web サービス クライアントと ASP.NET を使用して作成された XML Web サービスの間の通信中に、回線全体でデータの伝送に使用するプロトコルを定義するクラスで構成されています。

これらのオブジェクトの使用を促進するには、まず名前空間をディレクティブとして Class1.cs ファイルに追加します。 このようにして、このディレクティブを使用する場合、名前空間内の型を完全修飾する必要はありません。

  1. これらのディレクティブを追加するには、Class1.cs ファイルのコードの先頭に次のコードを追加します。 using System:
  
using SampleApplication.ExcelWebService;
using System.Web.Services.Protocols;
  
Imports SampleApplication.ExcelWebService
Imports System.Web.Services.Protocols

Web サービスを呼び出すには

  1. の開始角かっこ static void Main(string[] args)の後に次のコードを追加して、Web サービス プロキシ オブジェクトをインスタンス化して初期化します。
  
ExcelService es = new ExcelService();
  Dim es As New ExcelService()
  1. 次のコードを追加して、状態の配列および範囲座標オブジェクトを作成します。
  Status[] outStatus;
RangeCoordinates rangeCoordinates = new RangeCoordinates();
  
Dim outStatus() As Status
Dim rangeCoordinates As New RangeCoordinates()
  1. アクセスするワークシートを指すコードを追加します。 この例では、ワークシートは「Sheet1」といいます。 コードに次を追加します。

    
    string sheetName = "Sheet1";
    
    Dim sheetName As String = "Sheet1"
    

    注:

    開くブックに、値を含む「Sheet1」というワークシートがあるようにしてください。 あるいは、コード内の「Sheet1」を存在するワークシート名に変更することができます。

  2. 開くブックを指すように、次のコードを追加します。

    string targetWorkbookPath = "http://myserver02/example/Shared%20Documents/Book1.xlsx";
    
    Dim targetWorkbookPath As String = "http://myserver02/example/Shared%20Documents/Book1.xlsx"
    

    重要

    このチュートリアルで使用しているブックの場所に一致するようにブックのパスを変更します。 ブックが存在し、ブックの保存場所が信頼できる場所であるようにします。 ブックの場所を指す HTTP URL を使用すると、リモートでアクセスすることができます。

    注:

    ブックを右クリックして [ショートカットのコピー] を選択すると、Microsoft SharePoint Server 2010 でブックへのパスを取得できます。 または、[プロパティ] を選択してそこからブックへのパスをコピーすることもできます。

  3. 次のコードを追加して、要求の資格情報を設定します。

    注:

    既定の資格情報を使用する場合でも、資格情報を明示的に設定する必要があります。

  es.Credentials = System.Net.CredentialCache.DefaultCredentials;
  es.Credentials = System.Net.CredentialCache.DefaultCredentials
  1. 次のコードを追加して、ブックを開き、ブックが配置されている信頼できる場所を指定します。 このコードは try ブロックに配置します。
  try
{
string sessionId = es.OpenWorkbook(targetWorkbookPath, "en-US", 
    "en-US", out outStatus);
  
Try
Dim sessionId As String = es.OpenWorkbook(targetWorkbookPath, "en-US", "en-US", outStatus)
  1. 次のコードを追加して、範囲座標を定義し、 GetRange メソッドを呼び出すオブジェクトを準備します。 このコードは、範囲内の行数の合計と、特定の範囲の値を出力することもします。
  
rangeCoordinates.Column = 3;
rangeCoordinates.Row = 9;
rangeCoordinates.Height = 18;
rangeCoordinates.Width = 12;

object[] rangeResult1 = es.GetRange(sessionId, sheetName,
    rangeCoordinates, false, out outStatus);
Console.WriteLine("Total rows in range: " + rangeResult1.Length);
Console.WriteLine("Value in range is: " + ((object[])rangeResult1[5])[2]);
  
rangeCoordinates.Column = 3
rangeCoordinates.Row = 9
rangeCoordinates.Height = 18
rangeCoordinates.Width = 12

Dim rangeResult1() As Object = es.GetRange(sessionId, sheetName, rangeCoordinates, False, outStatus)
Console.WriteLine("Total rows in range: " &amp; rangeResult1.Length)
Console.WriteLine("Value in range is: " &amp; (CType(rangeResult1(5), Object()))(2))
  1. ブックを閉じ、現在のセッションを閉じるコードを追加します。 また、 try ブロックを終了する右中かっこを追加します。

    重要: セッションの使用が完了したら、ブックを閉じることをお勧めします。 これにより、セッションが終了し、リソースが解放されます。

  
es.CloseWorkbook(sessionId);
}
  
es.CloseWorkbook(sessionId)
  1. SOAP 例外をキャッチし、例外メッセージを出力する catch ブロックを追加します。
  catch (SoapException e)
{
    Console.WriteLine("SOAP Exception Message: {0}", e.Message);
}
  
Catch e As SoapException
    Console.WriteLine("SOAP Exception Message: {0}", e.Message)
End Try

完全なコード

次のコード サンプルは、ここまでの手順で作成した Class1.cs サンプル ファイルの完全なコードです。

重要: ブックのパスやシート名などは、適宜変更してください。


using System;
using SampleApplication.ExcelWebService;
using System.Web.Services.Protocols;

namespace SampleApplication
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {            
            // Instantiate the Web service and create a status array object and range coordinate object
            ExcelService es = new ExcelService();
            Status[] outStatus;
            RangeCoordinates rangeCoordinates = new RangeCoordinates();
            
string sheetName = "Sheet1";
            // Using workbookPath this way will allow 
            // you to call the workbook remotely.
            // TODO: change the workbook path to 
            // point to workbook in a trusted location
            // that you have access to 
            string targetWorkbookPath = "http://myserver02/example/Shared%20Documents/Book1.xlsx";
//you can also use .xlsb files, for example, //"http://myserver02/example/Shared%20Documents/Book1.xlsb";

            // Set credentials for requests
            es.Credentials = System.Net.CredentialCache.DefaultCredentials;
            //Console.WriteLine("Cred: {0}", es.Credentials);
            try
            {
                // Call open workbook, and point to the trusted   
                // location of the workbook to open.
                string sessionId = es.OpenWorkbook(targetWorkbookPath, "en-US", "en-US", out outStatus);
                // Console.WriteLine("sessionID : {0}", sessionId);

                // Prepare object to define range coordinates
                // and the GetRange method.
                rangeCoordinates.Column = 3;
                rangeCoordinates.Row = 9;
                rangeCoordinates.Height = 18;
                rangeCoordinates.Width = 12;

                object[] rangeResult1 = es.GetRange(sessionId, sheetName, rangeCoordinates, false, out outStatus);
                Console.WriteLine("Total Rows in Range: " + rangeResult1.Length);
                Console.WriteLine("Value in range is: " + ((object[])rangeResult1[5])[3]); 
        
                // Close workbook. This also closes session.
                es.CloseWorkbook(sessionId);
            }
            catch (SoapException e)
            {
                Console.WriteLine("SOAP Exception Message: {0}", e.Message);
            }
            // catch (Exception e)
//            {
//                Console.WriteLine("Exception Message: {0}", e.Message);
//            }
//            Console.ReadLine();
        }
    }
}
     

Imports System
Imports SampleApplication.ExcelWebService
Imports System.Web.Services.Protocols

Namespace SampleApplication
    Friend Class Class1
        <STAThread> _
        Shared Sub Main(ByVal args() As String)
            ' Instantiate the Web service and create a status array object and range coordinate object
            Dim es As New ExcelService()
            Dim outStatus() As Status
            Dim rangeCoordinates As New RangeCoordinates()

Dim sheetName As String = "Sheet1"
            ' Using workbookPath this way will allow 
            ' you to call the workbook remotely.
            ' TODO: change the workbook path to 
            ' point to workbook in a trusted location
            ' that you have access to 
            Dim targetWorkbookPath As String = "http://myserver02/example/Shared%20Documents/Book1.xlsx"
'you can also use .xlsb files, for example, //"http://myserver02/example/Shared%20Documents/Book1.xlsb";

            ' Set credentials for requests
            es.Credentials = System.Net.CredentialCache.DefaultCredentials
            'Console.WriteLine("Cred: {0}", es.Credentials);
            Try
                ' Call open workbook, and point to the trusted   
                ' location of the workbook to open.
                Dim sessionId As String = es.OpenWorkbook(targetWorkbookPath, "en-US", "en-US", outStatus)
                ' Console.WriteLine("sessionID : {0}", sessionId)

                ' Prepare object to define range coordinates
                ' and the GetRange method.
                rangeCoordinates.Column = 3
                rangeCoordinates.Row = 9
                rangeCoordinates.Height = 18
                rangeCoordinates.Width = 12

                Dim rangeResult1() As Object = es.GetRange(sessionId, sheetName, rangeCoordinates, False, outStatus)
                Console.WriteLine("Total Rows in Range: " &amp; rangeResult1.Length)
                Console.WriteLine("Value in range is: " &amp; (CType(rangeResult1(5), Object()))(3))

                ' Close workbook. This also closes session.
                es.CloseWorkbook(sessionId)
            Catch e As SoapException
                Console.WriteLine("SOAP Exception Message: {0}", e.Message)
            ' Catch e As Exception
            '    Console.WriteLine("Exception Message: {0}", e.Message)
            End Try
            'Console.ReadLine()
        End Sub
    End Class
End Namespace

関連項目

概念

SOAP API にアクセスする

その他のリソース

ステップ 1: Web サービス クライアント プロジェクトを作成する

手順 2: Web 参照を追加する

手順 4: アプリケーションのビルドとテストを行う

チュートリアル: Excel Web Services を使用してカスタム アプリケーションを開発する

[方法] スクリプトを使用してブックの場所を信頼する