Free Support Forum -

LoadFormat.Xlsx and SaveFormat.Xlsx produce Unable to read beyond the end of the stream error

I am having some issues with LoadFormat.Xlsx and SaveFormat.Xlsx. I am streaming documents from a database that are Excel 2007 format. When I attempt to open a Workbook using a stream, I get an error: "Unable to read beyond the end of the stream". If I remove the LoadOptions of LoadFormat.Xlsx it will work, but then I can't read certain parts of my document due to the column limitation of lower versions (97 - 2003). I have included some sample code below to reproduce the issue with the attached file. Thanks!

static void Main(string[] args) {

Aspose.Cells.LoadOptions opt = new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.Xlsx);
Aspose.Cells.Workbook workbook1 = new Aspose.Cells.Workbook(@"C:\code\WebTemplate2.xlsx", opt);
Console.WriteLine("workbook1 loaded");

System.IO.MemoryStream stream = new System.IO.MemoryStream();
//stream = workbook1.SaveToStream();
workbook1.Save(stream, Aspose.Cells.SaveFormat.Xlsx);

Aspose.Cells.Workbook workbook2 = new Aspose.Cells.Workbook(stream, opt); //ERROR OCCURS HERE
Console.WriteLine("workbook2 loaded");




After calling workbook.Save(Stream stream), the position of this stream will be at the end, if you want to open a workbook with the stream, you need add the below code:


stream.Seek(0, SeekOrigin.Begin);