[方法] 範囲のアドレスとシート名を指定する

この例では、範囲の座標、名前付き範囲、行、および列によって範囲のアドレスを指定する方法を示します。また、シート名とシート名および範囲のアドレス間のリレーションシップを指定する方法も示します。

範囲の座標は、連続した範囲の選択に使用する 4 つの整数座標です。範囲の座標では、"A1" 式の代わりに直接整数インデックス処理を使用して Excel の範囲を指定することができます。上行、左列、高さ、および幅の座標を指定できます。一連のセルをループで繰り返すコードを使用する場合、またはアルゴリズムの一部として範囲の座標が動的に計算される場合は、範囲の座標を使用する方が簡単です。

Excel Web Services は "現在のシート" を認識しないので、範囲の指定にはシート名を含める必要があります。シート名を指定する方法には次のような方法があります。

  • "Sheet3!B12:D18" のように、範囲のアドレスの一部として指定します。この場合、シート名の引数は空になります。

    C#
    object[] rangeResult1 = xlservice.GetRangeA1(sessionId, String.Empty, "Sheet2!A12:G18", true, out outStatus);
  • 独立したシート名の引数として指定します。この場合、範囲アドレスの引数にシート名を含める必要はありません。

    C#
    xlservice.SetCell(sessionId, "Sheet3", 0, 11, 1000);
  • シート名と範囲アドレスの両方で指定します。この場合、シートの名前は一致していなければなりません。

    C#
    object[] rangeResult = xlservice.GetCellA1(sessionId, "Sheet3", "Sheet3!G18", true, out outStatus);

名前付き範囲にはブックの範囲がある場合があるので、シート名を指定する必要がないのは名前付き範囲だけです。たとえば、次のようにシート名の引数を指定せずに名前付き範囲を参照することができます。

C#
xlServices.SetCellA1(sessionId, String.Empty, "MyNamedRange", 8);

シート名を指定する場合、参照する範囲は指定したシート上に存在する必要があります。存在しないシートを指定した場合、呼び出しが失敗し、シートが存在しないことを示す簡易オブジェクト アクセス プロトコル (SOAP) の例外が表示されます。

メモ メモ :

既に SharePoint ドキュメント ライブラリを作成し、それを信頼できる場所にしたことを前提としています。詳細については、「[方法] 場所を信頼する」と「[方法] スクリプトを使用してブックの場所を信頼する」を参照してください。

C#
using System;
using System.Text;
using System.Web.Services.Protocols;
using ExcelWebService.myserver02;
namespace ExcelWebService
{
/// <summary>
/// Summary description for Class1.
/// </summary>
    class MyExcelWebService
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Instantiate the Web service 
            // and range coordinate array object.
            ExcelService xlservice = new ExcelService();
            Status[] outStatus;
            RangeCoordinates rangeCoordinates = new RangeCoordinates();
            string sheetName = "MySheet1";

            // TODO: Change the path to the workbook
            // to point to a workbook you have access to.
            // The workbook must be in a trusted location.
            // Using the workbook path this way will allow 
            // you to call the workbook remotely.
            string targetWorkbookPath = 
       "http://myserver02/example/Shared%20Documents/MyWorkbook1.xlsx";

            // Set Credentials for requests
            xlservice.Credentials = 
                System.Net.CredentialCache.DefaultCredentials;

            try
            {
                // Call the open workbook, and point to    
                // the workbook to open.
                string sessionId = 
                    xlservice.OpenWorkbook(targetWorkbookPath, 
                        String.Empty, String.Empty, out outStatus);
                // Prepare object to define range coordinates
                // and call the GetRange method.
                // startCol, startRow, startHeight, and startWidth
                // get their values from user input.
                rangeCoordinates.Column = (int)startCol.Value;
                rangeCoordinates.Row = (int)startRow.Value;
                rangeCoordinates.Height = (int)startHeight.Value;
                rangeCoordinates.Width = (int)startWidth.Value;
                object[] rangeResult1 = xlservice.GetRange(sessionId, 
                    sheetName, rangeCoordinates, false, out outStatus);
                Console.WriteLine("Total rows in range: " + 
                    rangeResult1.Length);
                Console.WriteLine("Sum in last column is: " + 
                    ((object[])rangeResult1[18])[11]);

                // Call the SetCell method, which invokes 
                // the Calculate method.
                // Set first row in last column cell to 1000.
                xlservice.SetCell(sessionId, sheetName, 0, 11, 1000);

                // Call the GetRange method again to see if 
                // the Sum total in the last column changed.
                object[] rangeResult2 = xlservice.GetRange(sessionId, 
                    sheetName, rangeCoordinates, false, out outStatus);    
                Console.WriteLine("Sum in the last column after SetCell 
                    is: " + ((object[])rangeResult2[18])[11]); 

                // Close workbook. This also closes the session.
                xlservice.CloseWorkbook(sessionId);
            }

            catch (SoapException e)
            {
                Console.WriteLine("Exception Message: {0}", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception Message: {0}", e.Message);
            }
            Console.ReadLine();
        }
    }
}

堅牢なプログラミング

アクセス権のある Excel Web Services サイトに Web 参照が追加されていることを確認します。次のように変更します。

  • 参照する Web サービスを指すように using ExcelWebService.myserver02; ステートメントを変更します。

  • アクセス権のあるブックを指すように string targetWorkbookPath = "http://myserver02/example/Shared%20Documents/Book1.xlsx"; を変更します。ブックは信頼できる場所になければなりません。

関連項目

Page view tracker