We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Problem with SaveToStream() causing corrupted workbook

My application requires that I write workbooks to a database rather than the filesystem. When I read the workbook from the database, it has some significant differences from the workbook I actually generated. The workbook has a number of worksheets, each of which contains a line chart, a bar chart, or a column chart. I've noticed the following issues with the charts:

1) Colors are different from what I specified

2) Fonts are different from what I specified

3) All datapoint labels are missing

In attempting to isolate the problem, I tried converting the workbook to a byte array and then streaming it to the browser. The results in a workbook with the issues listed above. My code for doing this is:

// Stream file to browser.

System.IO.MemoryStream ms = workbook.SaveToStream();

response.ClearHeaders();

response.Clear();

response.AddHeader("Content-Disposition", "inline; filename=" + fileName);

response.ContentType = "application/octet-stream";

response.BinaryWrite(ms.ToArray());

response.End();

When I output directly to the browser using the Workbook.Save(..) method, the workbook is fine. Here's the code I use for this:

// Output to browser.

XlsSaveOptions saveOptions = new XlsSaveOptions();

saveOptions.SaveFormat = SaveFormat.Xlsx;

workbook.Save(response, fileName, ContentDisposition.Attachment, saveOptions);

It appears that the database is not what's causing the problem. Rather, it seems to have to do with my use of the Workbook.SaveToStream() method. Any idea what the problem could be? Thanks for your help.

Rick Cusworth

Hi Rick,

Well, Workbook.SaveToStream is used to save the workbook to XLS format and not for XLSX (Excel 2007) format.

So, you need to change your code a bit, e.g

//Save the workbook to stream
MemoryStream ms = new MemoryStream();
workbook.Save(ms, SaveFormat.Xlsx);
ms.Seek(0, SeekOrigin.Begin);


Also, there is some error for your other code segment regarding saving XLSX format, see the corrected code, e.g

OoxmlSaveOptions saveoptions = new OoxmlSaveOptions();
saveoptions.SaveFormat = SaveFormat.Xlsx;


Thank you.

Hi Amjad,

I made your suggested changes (along with changing my ContentType to "application/vnd.openxmlformats") and my app is now working correctly. Thanks for getting back to me so quickly.