簡體   English   中英

使用EPPlus多次寫入同一個excel文件

[英]Writing to the same excel file multiple times using EPPlus

我也在CodePlex討論板上發布了這個問題( https://epplus.codeplex.com/discussions/468568

下面是我在那里的復制和粘貼:

我試圖使用它來讀取/寫入SSIS中的Excel文檔。 64位模式下的SSIS不支持Excel連接。 發生的事情是在寫入同一個excel文件的不同任務中,第二個Save()總是失敗。 我也能在.NET中重現這一點。 以下模擬了我對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();
}

第二個保存返回以下錯誤:

{"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()

我嘗試加載源代碼以通過它進行調試,但是Cells集合不再存在。 我不確定那是怎么回事。 任何幫助,將不勝感激。

您可以對兩個保存使用相同的使用。 您只需打開一次文件,然后只獲取/使用另一張紙。 這可能會修復您的錯誤並提高您的性能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM