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

Free Support Forum - aspose.com

Cannot access cell values after loading data from a stream

I'm working on a generic exporter class for a web site. The class uses a LINQ query (provided in the constructor) and has a method ExportForDownload that takes an export type (Excel or PDF) and an optional list of properties to export from the results of the query. It returns a byte array containing the representation of the downloadable "file."

In my unit tests I take the byte array returned, write it to a memory stream, create a new Workbook and use LoadData with the stream as the argument to reconstitute the workbook. However, I'm unable to verify that the cells contain the correct data UNLESS I first write the workbook out to a file then read it back in. Is there some magic method I need to call to get the loaded data into the worksheet.

This works:

byte[] bytes = exporter.ExportForDownload(type, null);

MemoryStream byteStream = new MemoryStream();

byteStream.Write( bytes, 0, bytes.Length );

Workbook workbook = new Workbook();

workbook.LoadData( byteStream );

try

{

workbook.Save( "temp.xls" );

workbook.Open( "temp.xls" );

Cells cells = workbook.Worksheets[0].Cells;

Assert.AreEqual( 1, cells[0, 0].IntValue );

Assert.AreEqual( 2, cells[1, 0].IntValue );

Assert.AreEqual( 3, cells[2, 0].IntValue );

}

finally

{

File.Delete( "temp.xls" );

}


This doesn't (but should, I think):

byte[] bytes = exporter.ExportForDownload(type, null);

MemoryStream byteStream = new MemoryStream();

byteStream.Write( bytes, 0, bytes.Length );

Workbook workbook = new Workbook();

workbook.LoadData( byteStream );

Cells cells = workbook.Worksheets[0].Cells;

Assert.AreEqual( 1, cells[0, 0].IntValue );

Assert.AreEqual( 2, cells[1, 0].IntValue );

Assert.AreEqual( 3, cells[2, 0].IntValue );

Hi,

Thanks for providing your details.

We will get back to you for your query soon.

Thank you.

Hi,

We tried the following codes , it works fine:

FileStream fs = File.Open(@"F:\FileTemp\Book1.xls", FileMode.Open);
byte[] binary = new byte[fs.Length];
fs.Read(binary, 0, binary.Length);
MemoryStream ms = new MemoryStream();
ms.Write(binary, 0, binary.Length);


Workbook workbook = new Workbook();
workbook.LoadData(ms);
Console.WriteLine(workbook.Worksheets[0].Cells["A1"].IntValue);

And which version do you use? Please try the attached fix.

I'm using Aspose.Total 1.4.0.6 with hotfixes. I've downloaded the newest version but have not yet installed it. I was able to "fix" the problem by seeking to the beginning of the stream before loading the data.

This works for me with the current code:

byte[] bytes = exporter.ExportForDownload( type, null );

MemoryStream byteStream = new MemoryStream();

byteStream.Write( bytes, 0, bytes.Length );

byteStream.Seek( 0, SeekOrigin.Begin );

Workbook workbook = new Workbook();

workbook.LoadData( byteStream );

Cells cells = workbook.Worksheets[0].Cells;

Assert.AreEqual( 1, cells[0, 0].IntValue );

Assert.AreEqual( "f1", cells[0, 1].StringValue );

Assert.AreEqual( "l1", cells[0, 2].StringValue );

Assert.AreEqual( 2, cells[1, 0].IntValue );

Assert.AreEqual( "f2", cells[1, 1].StringValue );

Assert.AreEqual( "l2", cells[1, 2].StringValue );

Assert.AreEqual( 3, cells[2, 0].IntValue );

Assert.AreEqual( "f3", cells[2, 1].StringValue );

Assert.AreEqual( "l3", cells[2, 2].StringValue );