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");
Dim xlt As ExcelTemplate = 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");
Dim xla As ExcelApplication = New ExcelApplication
Dim wb As Workbook = 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. |