ASP.NET cells save method

When I run the follwong code in ASP.NET

objExcel.Save("ouputFile.xls", Cells.FileFormatType.Default, Cells.SaveType.OpenInBrowser, Response)
Label1.Text = "Hello"

The output file is properly created. However, Label1 text never changes.

Any ideas?

Hi,

objExcel.Save("ouputFile.xls", Cells.FileFormatType.Default, Cells.SaveType.OpenInBrowser, Response)

Well, this line defines the Response object will write the binary data through streams. It sends the buffered output to the client, stops the execuation of the current page and raises the Application _EndRequest event.

Thank you.

Is there a way to use

objExcel.Save("ouputFile.xls", Cells.FileFormatType.Default, Cells.SaveType.OpenInBrowser, Response)

in an asynchronus way so the page is refresh and the excel file is sent to the user?

Hi,

You should call Workbook.Save() method at the end of the your codes.

Or you can save the file to stream and send it to the user when you need.See following codes:

MemoryStream ms = workbook.SaveToStream();
byte[] buffer = new byte[ms.Length];
ms.Read(buffer, 0, buffer.Length);
this.Response.OutputStream.Write(buffer, 0, buffer.Length);

Can the stream be sent to a new browser window?

Hi,

Please try Response.Redirect method .