Writing to the same file multiple times

Nov 13, 2013 at 8:19 PM
I am attempting to use this to Read/Write excel documents within SSIS. SSIS in 64-bit mode does not support excel connections. What is happening is in different tasks that write to the same excel file the 2nd Save() always fails. I have been able to reproduce this in .NET as well. The following simulates what I have for a scenario in SSIS.
RowIndex = 2;
using (var ep = new ExcelPackage(new FileInfo(filePath)))
{                
            for (var i= 1; i<= 10; i++)
            {
                var worksheet = ep.Workbook.Worksheets["Sheet1"];
                worksheet.Cells[RowIndex, 1].Value = i;
                RowIndex++;
            }
            ep.Save();
}

RowIndex = 2;
using (var ep = new ExcelPackage(new FileInfo(filePath)))
{                
            for (var ii= 1; ii<= 10; ii++)
            {
                var worksheet = ep.Workbook.Worksheets["Sheet2"];
                worksheet.Cells[RowIndex, 1].Value = ii;
                RowIndex++;
            }
            ep.Save();
}
The second save returns the following error:
{"Error saving file C:\\####.xlsm"}
Inner Exception: 
   {"Index was outside the bounds of the array."}
      at OfficeOpenXml.Utils.CompoundDocument.GetChunk(Byte[] compBuffer, Int32& pos)
   at OfficeOpenXml.Utils.CompoundDocument.DecompressPart(Byte[] part, Int32 startPos)
   at OfficeOpenXml.VBA.ExcelVbaProject.ReadModules()
   at OfficeOpenXml.VBA.ExcelVbaProject.GetProject()
   at OfficeOpenXml.VBA.ExcelVbaProject..ctor(ExcelWorkbook wb)
   at OfficeOpenXml.ExcelWorkbook.get_VbaProject()
   at OfficeOpenXml.ExcelWorkbook.Save()
   at OfficeOpenXml.ExcelPackage.Save()
I tried loading the source code to debug through it however the Cells collection was no longer present. I am not sure how that happened. Any help would be appreciated.