HTTP Headers already written

I have a submit button on a page which runs some code to generate a merge and designer spreadsheet and then send it to the user. The code runs fine until it attempts to open. Then I receive an error about the http headers already being written. There isn't much to the code. If I run the code from the load event of the page it works fine. I'm also having the same problem if I open a spread directly from the server and try to save it to the user.

As a side note, on the same page I generate a designer word document and have no problem sending that to the user.

Function SendSpreadToUserFromStream(ByVal WorkBookName As Workbook, ByVal strFileName As String) As String

' Sends workbook which is contained in memory to user

WorkBookName.Save(strFileName, FileFormatType.Default, Aspose.Cells.SaveType.OpenInExcel, Response)

Return "Success"

End Function

Thanks for any help

Donna

Please check http://www.bellaonline.com/articles/art9737.asp .

Do you send the word file before sending Excel file? If yes, what happens if you send it after sending Excel file?

If you cannot figure it out, could you provide a sample project to reproduce your problem?

I looked at that page earlier it didn't seem to have any solutions that worked other than creating a blank aspx page with code behind and placing the code in the on load event. It works, but isn't really the optimum solution since I would end up with quite a few of these pages. Since the Aspose Words generates the docs correctly, it looks like the solution probably isn't redirecting to another page.

I pulled out some of the code from my app and created a test app with it. I tried it on our company dev server and had another developer in our team try it out and we received the same error. Just to rule out that the problem was my local development machine. The code needs to be opened as website and the starting page set to work correctly.

Should this work the same as Aspose Words does when saving to Word, when you save this to the user in Excel?

Thanks again.

Donna

Just a quick note. I did find if I add a try\catch to the save code, the spread will open in Excel, so the actual download of the file completes. At least I have a work around for now.

And you can also try following code to send the file to client:

//Saves file to memory
MemoryStream stream = new MemoryStream();
workbook.Save(stream, FileFormatType.Default);


response.ContentType = "application/vnd.ms-excel";


//This is same as OpenInExcel option
response.AddHeader( "content-disposition","attachment; filename=book1.xls");


//This is same as OpenInBrowser option
//response.AddHeader( "content-disposition","inline; filename=book1.xls");


response.BinaryWrite(stream.ToArray());

I figured out what the problem was. The click event for the button was firing twice, once off the aspx page and once from the code behind. I removed the serverclick event from the aspx page and that solved the problem.

Thanks for taking the time to try to help, it is much appreciated. Your support for your products is great.

Donna