It seems to me that you just need a way to programatically write Byte arrays to a WorkBook.
Here is a method that will write a byte[] as a sheet to a specific WorkBook:
public static void WriteToSheet(string targetBookPath, byte[] fileBytes)
{
try {
object x = Type.Missing;
//Create a temp file to encapsulate Byte array
string tmpPath = IO.Path.ChangeExtension(IO.Path.GetTempFileName(), ".xls");
My.Computer.FileSystem.WriteAllBytes(tmpPath, fileBytes, false);
//Start Excel Application (COM)
Excel.Application xlApp = new Excel.Application();
//Open target book
Excel.Workbook targetBook = xlApp.Workbooks.Open(targetBookPath, x, x, x, x, x, x, x, x, x,
x, x, x, x, x);
//Open temp file with Excel Interop
Excel.Workbook sourceBook = xlApp.Workbooks.Open(tmpPath, x, x, x, x, x, x, x, x, x,
x, x, x, x, x);
//Get a reference to the desired sheet
Excel.Worksheet sourceSheet = (Excel.Worksheet)sourceBook.Worksheets(1);
//Copy the temp sheet into WorkBook specified as "Before" parameter
Excel.Worksheet targetBefore = (Excel.Worksheet)targetBook.Worksheets(1);
try {
sourceSheet.Copy(targetBefore, x);
//Save and Close
sourceBook.Close(false, x, x);
targetBook.Close(true, x, x);
xlApp.Workbooks.Close();
xlApp.Quit();
}
catch (Exception ex) {
Debug.Fail(ex.ToString);
}
finally {
//Release COM objects
// Source
DESTROY(sourceSheet);
DESTROY(sourceBook);
// Target
DESTROY(targetBefore);
DESTROY(targetBook);
// App
DESTROY(xlApp);
}
//Kill the temp file
My.Computer.FileSystem.DeleteFile(tmpPath);
}
catch (Exception ex) {
Debug.Fail(ex.ToString);
}
}
The DESTROY method releases the COM stuff, which is pretty important:
public static void DESTROY(object o)
{
try {
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch (Exception ex) {
Debug.Fail(ex.ToString);
ErrorLog.Write(ex.ToString);
}
finally {
o = null;
}
}
If I understand correctly, all you would need to do is:
- Create a new WorkBook
- Loop through Byte arrays
- Call WriteToSheet for each Byte array