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 );