Free Support Forum - aspose.com

Error in detect MemoryStream of WorkBook

hi,
i write blow code, but it show error it latest line :exception {"Unable to read beyond the end of the stream."}
                 var ms = new MemoryStream();
                 _myWorkBook.Save(ms, SaveFormat.Auto);
                 var bytes = new byte[ms.Length];
                  ms.Read(bytes, 0, bytes.Length);
             <span style="color:blue;">var</span>  X = <span style="color:#2b91af;">CellsHelper</span>.DetectFileFormat(ms); //This Line Show Exception

---------------------------------------
if i Write EtcDataVariableRecognizedVariable = new byte [bytes.Length];
it Show this exception {ns4.Exception0: Cannot read that as a ZipFile —> ns4.Exception2:   Bad signature (0x00000000) at position  0x00000000\r\n   at ns4.Class5.smethod_8(Class5 class5_0, Encoding encoding_3)\r\n   at ns4.Class5.smethod_11(Class6 class6_1, Boolean bool_16)\r\n   at ns4.Class4.smethod_5(Class4 class4_0)\r\n   at ns4.Class4.smethod_1(Class4 class4_0)\r\n   — End of inner exception stack trace —\r\n   at ns4.Class4.smethod_1(Class4 class4_0)\r\n   at ns4.Class4.Read(Stream stream_2, TextWriter textWriter_1, Encoding encoding_2, EventHandler`1 eventHandler_5)\r\n   at ns4.Class4.Read(Stream stream_2)\r\n   at ns4.Class383.method_0(Stream stream_0)\r\n   at Aspose.Cells.Workbook.method_11(Stream stream_0)\r\n   at Aspose.Cells.Workbook.method_7(Stream stream_0, LoadOptions loadOptions_0, Boolean bool_2)\r\n   at Aspose.Cells.Workbook…ctor(Stream stream)\r\n   at 
-------------------------------------------
if i Write  ByteVariable = bytes;
it Shoe this exception {“Object reference not set to an instance of an object.”}

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please download and use the latest version: Aspose.Cells
for .NET v7.3.4.3


You need to set the MemoryStream.Postion to 0 before calling the DetectFileFormat method. I have tested your issue with the following code and it works fine.

Changes are highlighted in red color for your help.

C#


Workbook _myWorkBook = new Workbook();


var ms = new MemoryStream();

_myWorkBook.Save(ms, SaveFormat.Auto);

var bytes = new byte[ms.Length];

ms.Read(bytes, 0, bytes.Length);


ms.Position = 0;

var X = CellsHelper.DetectFileFormat(ms); //This line works fine now.