Reputation: 21
I'm working with a C1XLBook object that I want to save to a memory stream so I can download it. The code is as follows (memory stream and workbook were declared higher up):
workbook.Save(ms);
byte[] content = ms.GetBuffer();
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "application/vnd.ms-excel";
string contentDisposition = "inline";
string filename = "Worksbook.xls";
Response.AddHeader("Content-Disposition", contentDisposition + "; filename=\"" + filename + "\"");
Response.AddHeader("Content-Length", content.Length.ToString());
Response.BinaryWrite(content);
Response.End();
The result is that a file gets downloaded by the client, but it ends up looking like this:
However, when I save the file to my local system it comes out looking how I would expect. Are there any thoughts for how this can be fixed? I've been doing all kinds of research but I haven't found the answer yet.
I expected the data I had to get exported into the excel file in human readable form. I've tried messing with the Response properties, I've tried changing the properties on the memory stream, I've messed with the encoding, but nothing seems to work.
Upvotes: 0
Views: 134
Reputation: 21
I don't know how this worked, but the way I solved it was simply to remove Response.ClearHeaders()
and it started working. If anyone knows why it worked I'd love to know... Seems weird that it'd be that simple.
Upvotes: 0