如果我们要把创作完工作簿文件副本存放在某处,然后发送给某人,那么最好的方式就是使用Excel Web Services。通过Excel Web Services我们可以得到一个工作簿或一个快照。
using System;
using System.IO;
using System.Text;
using System.Web.Services.Protocols;
// TODO: Change the using GetSnapshot.ExcelWebService; directive
// to point to the Web service you are referencing.
using
// GetSnapshot.ExcelWebService;
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,
// proactively releasing resources on the server.
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);
}
}
}
}