在将数据写入excel时,我收到错误System.OutOfMemoryException

时间:2016-05-03 21:37:44

标签: c# asp.net excel

调试时我收到错误' -`((Microsoft.Office.Interop.Excel.Range)(xlWorkSheet.Cells))。Value2'抛出了类型&System; On.OutOfMemoryException'的异常。我在下面的调试中指定了我收到错误的地方。我正在读取行和列,并且在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();
                }
            }

0 个答案:

没有答案