How to: Save a Workbook

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:

  • If you want to save the Excel file to a Windows SharePoint Services 3.0 document library, use the Windows SharePoint Services 3.0 object model or Web service.

  • If you want to save the Excel file to a stream, a local file, or a file share, use the Microsoft .NET Framework libraries.

  • If you want to save the Excel file to an HTTP location, use Web Distributed Authoring and Versioning (WebDAV).

Note

WebDAV is also known as the HTTP/DAV protocol or the DAV protocol.

You can use the various .NET Framework libraries to save a workbook. The following example shows one of the methods.

Example

The following sample code uses the Console.OpenStandardOutput method to acquire the standard output stream. It then uses the System.IO.BinaryWriter.Write method to write either an entire workbook or a snapshot file returned by the Web service's GetWorkbook method. It writes the snapshot to a file using stdout and the command-line filename argument as shown in the following code:

BinaryWriter binaryWriter = 
    new BinaryWriter(Console.OpenStandardOutput());
binaryWriter.Write(workbook);
binaryWriter.Close();

For more information about the OpenStandardOutput and Write methods, see the Microsoft .NET Framework class library documentation.

The following sample shows how to get a snapshot and then write it to standard output. For more information about snapshots, see How to: Get an Entire Workbook or a Snapshot topic. For more information about the GetWorkbook method, see the Excel Web Services reference documentation.

// Open the workbook, then call GetWorkbook 
// and close the session.
string sessionId = xlService.OpenWorkbook(args[0], "en-US", "en-US", out status);

// Get a full snapshot of the workbook.
byte[] workbook = xlService.GetWorkbook(sessionId, WorkbookType.FullWorkbook, out status);
                
 // Close 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();

See Also

Tasks

Step 1: Creating the Web Service Client Project
Step 2: Adding a Web Reference
Step 3: Accessing the Web Service
Step 4: Building and Testing the Application
Walkthrough: Developing a Custom Application Using Excel Web Services
How to: Trust a Location
How to: Trust Workbook Locations Using Script
How to: Enable UDFs

Concepts

Accessing the SOAP API