同时写入数据到Excel我得到错误System.OutOfMemoryException

在debugging时,我得到了错误'((Microsoft.Office.Interop.Excel.Range)(xlWorkSheet.Cells))。Value2'抛出了一个types为“System.OutOfMemoryException”的exception。 我在下面debugging的时候指定了出错的地方。 我正在读取行和列,并在Excel中的特定位置,我试图插入一个数据。

public static void Update_Daily_Resolution(string Daily_Resolution_Path, List<Summary> Flux, List<Summary> Active,List<Summary> OnHold) { xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open(Daily_Resolution_Path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item("April 15 - 21"); range = xlWorkSheet.UsedRange; for (int i = 1; i <= range.Rows.Count; i++) //updation of VMS { for (int j = 1; j <= 17; j++) { string str1 = (string)(range.Cells[i, j] as Excel.Range).Text; if (str1 == "VMS" && str1 != "RES") { int rCnt = i + 1; for (int cCnt = 1; cCnt <= 17; cCnt++)//looping through each column { string str2 = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Text; if (str2 == "20-Apr")//searching for the right column to update the data { int iCount = rCnt + 1;//fetching the row value int jCount = cCnt;//fetching the column value **xlWorkSheet.Cells[iCount, jCount] = Flux[0].VMS; //Here i am getting this error while debugging** xlWorkSheet.Cells[iCount + 2, jCount] = Convert.ToString(Convert.ToInt32(OnHold[0].VMS)-Convert.ToInt32(xlWorkSheet.Cells[iCount+4,jCount-1].Text)); //Tickets moved to hold xlWorkSheet.Cells[iCount+3, jCount] = Active[0].VMS; // Ending Backlog xlWorkSheet.Cells[iCount + 4, jCount] = OnHold[0].VMS; //Ending Hold } } } } } xlWorkBook.Save(); //MyBook.SaveCopyAs(Incident_Path); //xlWorkSheet.Close("Ageing"); xlApp.Quit(); //Call the garbage collector to collect and wait for finalizers to finish. GC.Collect(); GC.WaitForPendingFinalizers(); Marshal.ReleaseComObject(xlWorkSheet); Marshal.ReleaseComObject(xlWorkBook); Marshal.ReleaseComObject(xlWorkBook); Marshal.ReleaseComObject(xlApp); Process[] excelProcs = Process.GetProcessesByName("EXCEL"); //use to the Kill method of the .Net Process object. foreach (Process proc in excelProcs) { proc.Kill(); } }