Home     Products      Support      Corporate     Sign In 

Support Knowledge Base, Article 932

Product
ExcelWriter
Title
Saving a spreadsheet to disk and streaming it to the user in ASP.NET
Problem
Some applications require that a copy of the ExcelWriter-generated spreadsheet is saved on the server while the spreadsheet is also streamed to the users browser. This can be accomplished with ExcelWriter, but the approach is different depending on whether you are using the ExcelApplication object or the ExcelTemplate object. This article discussed the two approaches.
Solution

Using the ExcelTemplate object

In version 5 and above of ExcelWriter, the ExcelTemplate object provides the ability to easily save multiple copies of a spreadsheet. This is done using the Process and Save methods. The Process method builds your spreadsheet completely in memory. The Save method just takes the in-memory file and either writes it to the Response stream or the hard drive. Save can be called any number of times, so it is easy to save one or more copies of the file to disk and also stream it to the user.

Example:

[C#] [VB.NET]  
ExcelTemplate xlt = new ExcelTemplate();
xlt.Open("C:\\templates\in.xls");
// The rest of your code including all your calls to SetDataSource
// ....
// The Process method creates the xls file in memory
xlt.Process();
// Save a copy to disk by passing a complete physical path
// to a writable location on the server 
xlt.Save("C:\\OutputFiles\\out.xls");
// Stream a copy to the user using a different overload of the Save method
xlt.Save(Response,"out.xls");

Using the ExcelApplication object

Saving multiple copies of a spreadsheet is a little more complicated with the ExcelApplication object. This is because the rich object model of the application object allows you to continue processing parts of the file at any time until Save is called. There is no Process method to guarantee that the bytes of the spreadsheet are completely set before calling Save. The Save method in the application object does alot more than just copying an image of an in-memory file. It does much of the actual processing of the spreadsheet. Because of this, it is not possible to call Save multiple times with ExcelApplication object.

The way to save multiple copies of a file with the application object is to save a copy to disk with ExcelApplication's Save method and then use ASP.NET's Response.WriteFile method to stream a copy of the file to the user's browser.

Example

[C#] [VB.NET]  
ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create();  
// The rest of your code here 
// ...
// Save a copy to disk by passing a complete physical path to a
// writable location on the server
xla.Save(wb, "C:\\OutputFiles\\out.xls");
// Stream the file to the user with Response.WriteFile
// You will need to set some response headers as well
Response.ContentType="application/vnd.ms-excel";
Response.AddHeader("content-disposition","attachment; filename=out.xls");
Response.WriteFile("C:\\OutputFiles\\out.xls");

Note: Another option would be to save to a MemoryStream (using this overload of the Save method) and then write to disk and to the Response using .NET classes.

Created : 7/27/2007 4:08:45 PM (last modified : 10/2/2007 9:32:46 AM)

Rate this article!

 
Comments



Copyright 2006 © SoftArtisans, Inc. All Rights Reserved.

Site Map     |     Privacy Policy     |     Contact Us