Retrieving an Entire Workbook or a Snapshot of a Workbook in Excel Services
Summary: Learn how to retrieve an entire workbook or a snapshot of a workbook by using Excel Services.
Applies to: Microsoft Office SharePoint Server 2007, Microsoft Office Excel 2007
Siew Moi Khor, Danny Khen, Microsoft Corporation
Joel Krist, Akona Systems
April 2007
If you want to save a copy of an up-to-date workbook, store it somewhere, send it to someone, and so on, you can retrieve an entire workbook or a snapshot using Excel Web Services. When a user or application requests a snapshot, Excel Services opens the Excel file on the server, refreshes data sources, and calculates all Excel formulas. Excel Services then generates and sends a snapshot back through the Web service API. This example illustrates how to get an entire workbook, a snapshot of the entire file, or just a snapshot of the viewable sheets or objects in the file using Excel Web Services. Use the GetWorkbook method in Excel Web Services to get either the entire workbook or one of the snapshot types. Retrieving a Snapshot The following code returns a snapshot of the entire Excel workbook. It uses the WorkbookType.FullSnapshot enumeration as the second argument in the GetWorkbook method. The GetWorkbook method returns a byte array, in the same Excel file format as the one loaded into the session. Retrieving a Snapshot of Viewable Items To get a snapshot of the items that the Excel workbook author selected as viewable when saving the workbook from Excel to the server, use the WorkbookType.PublishedItemsSnapshot enumeration as shown here:
Retrieving an Entire Workbook To get a snapshot of the entire workbook in its current session state, use the WorkbookType.FullWorkbook enumeration. The WorkbookType.FullWorkbook and WorkbookType.FullSnapshot options work only if the user has open rights to the file; if the user has view-only rights, the call fails. For more information about the GetWorkbook method and the WorkbookType enumeration, see the Microsoft.Office.Excel.Server.WebServices reference documentation. Complete Code The following program (a console application) receives one command-line argument, which is the path to the workbook on the server. The program calls the Web service to open the workbook on the server and get a snapshot. It then writes it to standard output so that you can redirect it to a new snapshot file.
Running the application Use the following command line and arguments to run the GetSnapshot application:
For example:
If you use the previous command-line example, the GetSnapshot tool places a new file in the "C:\" directory. Note The workbook that you want to get a snapshot of must be in a trusted location. Robust Programming Make sure you add a Web reference to an Excel Web Services site you have access to. Change the A full workbook represents the current state of the workbook in the Excel Services session. Excel Calculation Services generates the full workbook based on the workbook that the session opened. It reflects all the modifications that have happened to this original workbook during the session: values that went into parameters, external data connections that were refreshed, and formulas that were calculated based on parameter and external data values. It is identical to what a user would get by selecting the Open in Excel command in Excel Web Access. A snapshot is a workbook generated by Excel Calculation Services, and it represents a "report" or a "result" of the current state; it contains everything you can see in the browser using Excel Web Access (and nothing else), but it is delivered in a workbook file format. Some snapshots (known as "published item snapshots") contain only those portions of the Excel file that an author selects as viewable when saving the file to the server. Snapshots contain the layout and formats of the original file, and up-to-date values calculated by Excel Calculation Services, but they do not contain Excel formulas or external data connections. You can acquire a snapshot of a workbook you already saved to the server, even if you do not have the rights to access the actual file on the server. In some cases, your code needs to save the result of a GetWorkbook call. Excel Web Services is a front-end interface to Excel Services. In general, Excel Services does not create, modify, or save files. It only reads them to start sessions. Therefore, Excel Web Services does not have a method to save a workbook. To save a workbook, you generally use the API of the destination file store. For example:
Note WebDAV is also known as the HTTP/DAV protocol or the DAV protocol. You can use the various Microsoft .NET Framework libraries to save a workbook. The following example shows one of the methods. For a discussion about how to save a workbook, see the How to: Save a Workbook example. |
Length: 10:40 | Size: 12.2 MB | Type: WMV file |