Saving instrance of excel object to a stream and display the spreadsheet in a new window


#1

I was hoping to find code for saving excel results to a stream to avoid using a hard drive and dealing with all security issues.I know there is a method in Aspose.Excel that allows to save to a stream excelObj.Save(stream, fileFormatType) but I could not find any code examples that would explain what to do with that stream afterwards. What I am trying to achive is to open a new window save the excel report to a stream, and display the excel output in that new window. If anyone had experience doing it or has ideas on how to do it I would appreciate the input.


#2

Is your application a desktop application or a web application?

If it's a desktop application, I think you have to save it to disk before opening it in a new Excel window.

If it's a web application, you can try this piece of sample code:

byte[] data = stream.ToArray();

this.Response.ContentType = "application/xls";
Response.AddHeader( "content-disposition","inline; filename=book1.xls");
Response.BinaryWrite(data);
Response.End();


#3

Laurence,

thanks for your prompt response. The app in question is a web app running on dotnetnuke platform. From the parent page I open a new window and call an .aspx module to recreate my report and display the output in excel. The problem I am running into when using your sample code is that the new window is still trying to open .aspx page and not the excel doc and therefore the browser is raising an error. I can save the output to a disk and then redirect the page to that excel file but this solution involves using a network drive and my goal is to try to avoid that. If you have any other thoughts let me know.

Thanks for your help

K


#4

Please try:

Response.AddHeader( "content-disposition","attachment; filename=book1.xls");


#5

Hi Laurence,

thanks for your input. I think I've made some progress with my problem. The line I was missing in my code was:

Response.ClearHeader()

This line needs to go before

Response.ContentType = "application/vnd.ms-excel"

Response.AddHeader("Content-Disposition", "inline; filename=report.xls")

Response.BinaryWrite(myStream.ToArray)

Response.End()

In my case it worked. The choice btw "inline" or "attachment" determines if excel spreadsheet will open in a separate excel window or will load in a browser window.

One issue I came across is when I have a sptreadsheet open in a browser window and try to save it the default setting in the "Save As" dialog box shows "Copy of Deafult.aspx" instead of reports.xls

Let me know if you have any thoughts on how to get rid of that default setting

Regards

Konstantin


#6

Since your file is created within default.aspx, the default file name will be “Copy of Deafult.aspx”. That’s a routine of IE and I haven’t find a way to change it.