2

recently I was trying to save Aspose.Cells.Workbook to stream with

private Stream GetWorkbook()
{
    // processing workbook here
    // ...
    // saving to stream
    return workbook.SaveToStream();
}

private void Save()
{
    using (stream = GetWorkbook())
        using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
        {
            stream.CopyTo(fileStream);
        }
}

But when I'm trying to open generated .xlsx file Excel sends me an error that file is corrupted.

Max
  • 93
  • 1
  • 2
  • 12
  • What is the file size of the xlsx file? If you rename the xlsx file to `a.zip` and open it up, is it a valid zip file? – mjwills Sep 05 '17 at 12:46
  • @mjwills Nope, it's not. The actual template file size is 120KB, generated file is 1KB. – Max Sep 05 '17 at 16:23
  • Does putting `stream.Seek(0, SeekOrigin.Begin);` before the `CopyTo` help? – mjwills Sep 05 '17 at 21:20

1 Answers1

1

SaveToStream() method will only save your workbook in XLS format. So you should not use this method but use the following code to save your workbook in memory stream object. It should fix your issue.

C#

//Load your Excel file
Workbook wb = new Workbook(yourFile);

//Create memory stream object
MemoryStream ms = new MemoryStream();

//Determine the save format
SaveFormat svfmt = (SaveFormat)wb.FileFormat;

//Save the workbook to memory stream
wb.Save(ms, svfmt);

Note: I am working as Developer Evangelist at Aspose

shakeel
  • 1,717
  • 10
  • 14