如何:获取范围中的值

上次修改时间: 2010年8月2日

适用范围: SharePoint Server 2010

Excel Web Services 公开四种用于从 Excel 工作簿获取值的方法:GetCell、GetCellA1、GetRange 和 GetRangeA1。

GetCell 和 GetCellA1 方法返回单个单元格的值。如果尝试请求多个单元格(例如,传入诸如"B1:E2"之类的区域引用或一个大于单个单元格的命名区域等),则方法调用将失败。如果想检索单元格区域中的值,请改用 GetRange 和 GetRangeA1 方法。

带有 A1 后缀的方法(GetCellA1 和 GetRangeA1)使用一种与没有此后缀的方法(GetCell 和 GetRange)不同的坐标系统。如果要以 Excel 样式对单元格进行引用,如范围引用(例如 H8、A3:D5、Sheet2!A12:G18)或命名范围,则应使用后缀为 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)

如果是从多个相邻的单元格中获取值,则可能要考虑使用 GetRange 方法,而不是多次调用 GetCell 方法。这样做将导致一次服务器往返,而不是多次往返。因此,在某些情况下,通过使用 GetRange 方法而不是 GetCell 方法,性能可能会显著改善。

使用 GetRange 和 GetRangeA1 方法获取单元格区域时,将返回一个对象数组(在 C# 中为 object[],在 Visual Basic .NET 中为 Object ())。这个对象数组实际上是一个交错数组。返回的数组中的每项将成为表示单元格的对象的另一个数组。有关交错数组的详细信息,请参阅交错数组(C# 编程指南)(该链接可能指向英文页面) (https://msdn.microsoft.com/zh-cn/library/2s05feca.aspx)(该链接可能指向英文页面)。

使用 GetCell 和 GetRange 方法获取值

  1. 使用 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)
    
  2. 使用 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 方法获取值

  1. 使用 GetCellA1 方法获取打开的工作簿中的单元格的值(使用 Excel"A1"区域规范);例如:

    // 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)
    
  2. 使用 GetRangeA1 方法获取打开的工作簿中的区域的值(使用 Excel"A1"区域规范)。下面的代码示例要求一个 2x3 区域(即两行乘三列)。代码随后会循环遍历返回的每一行,并检索每行包含的三个单元格。也就是说,在第一次迭代中:

    • rangeResult [0] 返回单元格 B2 中的值

    • rangeResult [1] 返回单元格 C2 中的值

    • rangeResult [2] 返回单元格 D2 中的值

      在第二次迭代中:

    • 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 开发自定义应用程序

概念

访问 SOAP API