如何:获取整个工作簿或快照

上次修改时间: 2010年3月24日

适用范围: SharePoint Server 2010

本示例演示了如何通过使用 Excel Web Services 获取整个工作簿、整个文件的快照,或只获取文件中可查看工作表或对象的快照。如果您想保存最新工作簿的一份副本、将其存储在某个位置、将其发送给某人等等,那么获取工作簿或快照将非常有用。

快照是由 Excel Calculation Services 生成的工作簿,它表示工作簿在 Excel Services 会话中的当前状态。一些快照(称为"发布项快照")只包含那些作者在将文件保存到服务器时选择为可查看的 Excel 文件部分。快照包含原始文件的布局和格式以及由 Excel Calculation Services 计算的最新值,但不包含 Excel 公式或外部数据连接。Excel Services 将打开服务器上的 Excel 文件,刷新数据源,并计算所有 Excel 公式。当用户或应用程序请求快照时,Excel Services 将随即通过 Web 服务 API 生成并发回一个快照。

您可以获得已保存到服务器的工作簿的快照,即便您不具有服务器上实际文件的访问权限也可以获得该快照。

您可以使用 Web 服务的 GetWorkbook 方法来获取整个工作簿或其中一个快照类型。例如,以下代码将返回整个 Excel 工作簿的快照。它使用 WorkbookType.FullSnapshot 枚举作为 GetWorkbook 方法中的第二个参数。

byte[] workbook = xlService.GetWorkbook(sessionId, WorkbookType.FullSnapshot, out status);
Dim workbook() As Byte = xlService.GetWorkbook(sessionId, WorkbookType.FullSnapshot, status)

GetWorkbook 方法将返回一个字节数组,它与加载到该会话中的 Excel 文件格式相同。

若要获取 Excel 工作簿作者将工作簿从 Excel 保存到服务器时选择为可查看的项目的快照,可使用以下所示的 WorkbookType.PublishedItemsSnapshot 枚举:

byte[] workbook = xlService.GetWorkbook(sessionId, WorkbookType.PublishedItemsSnapshot, out status);
Dim workbook() As Byte = xlService.GetWorkbook(sessionId, WorkbookType.FullSnapshot, status)

若要获取处于当前会话状态的整个工作簿的快照,可使用 WorkbookType.FullWorkbook 枚举:

byte[] workbook = xlService.GetWorkbook(sessionId, WorkbookType.FullWorkbook, out status);
Dim workbook() As Byte = xlService.GetWorkbook(sessionId, WorkbookType.FullWorkbook, status)

只有当用户具有文件的打开权限时,才可以使用 WorkbookType.FullWorkbook 选项;如果用户具有仅查看权限,则调用将失败。

在某些情况下,您的代码需要保存 GetWorkbook 调用的结果。有关如何保存工作簿的讨论,请参阅How to: Save a Workbook示例。

有关 GetWorkbook 方法和 WorkbookType 枚举的详细信息,请参阅 Excel Web Services 参考文档。

示例

以下程序(控制台应用程序)将接收一个命令行参数,该参数是工作簿在服务器上的路径。程序将调用 Web 服务以打开服务器上的工作簿并获取一个快照。然后将其写入标准输出,以便您可将其重定向到新的快照文件。

using System;
using System.IO;
using System.Text;
using System.Web.Services.Protocols;
// TODO: Change the using GetSnapshot.myServer02 statement 
// to point to the Web service you are referencing.
using GetSnapshot.myServer02;

namespace GetSnapshot
{
    class ExcelServicesSnapshot
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length < 1)
                {
                    Console.Error.WriteLine("Command line arguments should be: GetSnapshot [workbook_path] > [snapshot_filename]");
                    return;
                }
                // Instantiate the Web service and 
                // create a status array object.
                ExcelService xlService = new ExcelService();
                Status[] status;
                
                xlService.Timeout = 600000;
                // Set credentials for requests.
                // Use the current user's logon credentials.
                xlService.Credentials =   
                    System.Net.CredentialCache.DefaultCredentials;
                 
                // Open the workbook, then call GetWorkbook 
                // and close the session.
                string sessionId = xlService.OpenWorkbook(args[0], "en-US", "en-US", out status);
                byte[] workbook = xlService.GetWorkbook(sessionId, WorkbookType.PublishedItemsSnapshot, out status);
                // byte[] workbook = xlService.GetWorkbook(sessionId, WorkbookType.FullWorkbook, out status);
                // byte[] workbook = xlService.GetWorkbook(sessionId, WorkbookType.FullSnapshot, out status);

                // Close the workbook. This also closes the session.
                status = xlService.CloseWorkbook(sessionId);

                // Write the resulting Excel file to stdout 
                // as a binary stream.
                BinaryWriter binaryWriter = new BinaryWriter(Console.OpenStandardOutput());
                binaryWriter.Write(workbook);
                binaryWriter.Close();
            }

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

            catch (Exception e)
            {
                Console.WriteLine("Exception Message: {0}", e.Message);
            }   
        } 
    }
}
Imports System
Imports System.IO
Imports System.Text
Imports System.Web.Services.Protocols
' TODO: Change the using GetSnapshot.myServer02 statement 
' to point to the Web service you are referencing.
Imports GetSnapshot.myServer02

Namespace GetSnapshot
    Friend Class ExcelServicesSnapshot
        Shared Sub Main(ByVal args() As String)
            Try
                If args.Length < 1 Then
                    Console.Error.WriteLine("Command line arguments should be: GetSnapshot [workbook_path] > [snapshot_filename]")
                    Return
                End If
                ' Instantiate the Web service and 
                ' create a status array object.
                Dim xlService As New ExcelService()
                Dim status() As Status

                xlService.Timeout = 600000
                ' Set credentials for requests.
                ' Use the current user's logon credentials.
                xlService.Credentials = System.Net.CredentialCache.DefaultCredentials

                ' Open the workbook, then call GetWorkbook 
                ' and close the session.
                Dim sessionId As String = xlService.OpenWorkbook(args(0), "en-US", "en-US", status)
                Dim workbook() As Byte = xlService.GetWorkbook(sessionId, WorkbookType.PublishedItemsSnapshot, status)
                ' byte[] workbook = xlService.GetWorkbook(sessionId, WorkbookType.FullWorkbook, out status);
                ' byte[] workbook = xlService.GetWorkbook(sessionId, WorkbookType.FullSnapshot, out status);

                ' Close the workbook. This also closes the session.
                status = xlService.CloseWorkbook(sessionId)

                ' Write the resulting Excel file to stdout 
                ' as a binary stream.
                Dim binaryWriter As New BinaryWriter(Console.OpenStandardOutput())
                binaryWriter.Write(workbook)
                binaryWriter.Close()

            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
        End Sub
    End Class
End Namespace

使用以下命令行和参数运行 GetSnapshot 应用程序:

GetSnapshot.exe [workbook_path] > [snapshot_filename]

例如:

C:\>GetSnapshot.exe http://myServer02/reports/reports/OriginalWorkbook.xlsx > SnapshotCopy.xlsx

如果您使用之前的命令行示例,GetSnapshot 工具将在"C:\"目录下放一个新文件。

备注

您要获取其快照的工作簿必须位于受信任的位置。

强大的编程功能

请确保添加您有权访问的 Excel Web Services 网站的 Web 引用。更改 using GetSnapshot.myServer02; 语句以指向您引用的 Web 服务网站。

请参阅

任务

步骤 1:创建 Web 服务客户端项目

步骤 2:添加 Web 引用

步骤 3:访问 Web 服务

步骤 4:生成和测试应用程序

演练:使用 Excel Web Services 开发自定义应用程序

如何:信任一个位置

概念

访问 SOAP API