1) GC.Collect(); // this one forces garbage collection
2) System.Runtime.InteropServices.Marshal.ReleaseComObject (object); // this one releases the passed in COM object wrapper instanceThe pattern the author (Peter A. Bromberg) uses to create and release COM Wrapped Excel objects is :-
1) call GC.Collect() to force collection of existing COM Objects waiting to be released.
2) instantiate the COM Wrapped Excel objects (Workbook, Worksheet etc.)
3) do the thing...
4) call the Close() or Quit() methods on the objects when done.
5) call System.Runtime.InteropServices.Marshal.ReleaseComObject(object) once for each COM object created.
6) set each object variable to null.
7) call GC.Collect() again
8) be relieved and reminisce the cool VB6 way of doing the above (Set obj = Nothing)Here is a slightly altered & annotated code fragment (in C#) that shows how it is done :-Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;// Step 1
GC.Collect();// clean up any other excel guys hangin' around...// Step 2
oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;// Step 3
// this part will actually be filling in the values into the sheet
fillValues(oSheet);
....// Step 4
// Need all following code to clean up and extingush all references!!!
oWB.Close(null,null,null);
oXL.Workbooks.Close();
oXL.Quit();// Step 5
System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);// Step 6
oSheet=null;
oWB=null;
oXL = null;// Step 7
GC.Collect(); // force final cleanup!
2) System.Runtime.InteropServices.Marshal.ReleaseComObject (object); // this one releases the passed in COM object wrapper instanceThe pattern the author (Peter A. Bromberg) uses to create and release COM Wrapped Excel objects is :-
1) call GC.Collect() to force collection of existing COM Objects waiting to be released.
2) instantiate the COM Wrapped Excel objects (Workbook, Worksheet etc.)
3) do the thing...
4) call the Close() or Quit() methods on the objects when done.
5) call System.Runtime.InteropServices.Marshal.ReleaseComObject(object) once for each COM object created.
6) set each object variable to null.
7) call GC.Collect() again
8) be relieved and reminisce the cool VB6 way of doing the above (Set obj = Nothing)Here is a slightly altered & annotated code fragment (in C#) that shows how it is done :-Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;// Step 1
GC.Collect();// clean up any other excel guys hangin' around...// Step 2
oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;// Step 3
// this part will actually be filling in the values into the sheet
fillValues(oSheet);
....// Step 4
// Need all following code to clean up and extingush all references!!!
oWB.Close(null,null,null);
oXL.Workbooks.Close();
oXL.Quit();// Step 5
System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);// Step 6
oSheet=null;
oWB=null;
oXL = null;// Step 7
GC.Collect(); // force final cleanup!
当在服务器本地用浏览器运行时,
以上运行都正常,然后程序指向这个生成的EXCEL文件时,就出错了
“内存不能为read”,然后进程就锁死了
Excel.Application myExcel;
Excel._Workbook myWB;
Excel._Worksheet myWS = null;
Excel.Range myRrange = null;
try
{
myExcel = new Excel.ApplicationClass();
myWB = (Excel._Workbook)myExcel.Workbooks.Open(file.FullName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
}
catch(Exception e)
{
e.ToString();
}
int SheetNum = myWB.Worksheets.Count;
if(SheetNum >0)
{
for(int i=1;i<=SheetNum;i++)
{
myWS = (Excel._Worksheet)myWB.Worksheets[i]; int RowNum = myWS.UsedRange.Cells.Rows.Count;
if(RowNum >= 2)
{
for(int j=2;j<=RowNum;j++)
{
myRrange = myWS.get_Range("A"+j.ToString(), "E"+j.ToString());
Array myvalues = (Array)myRrange.Cells.Value;
}
}
}//以下内容全都不能少!!
myWB.Close(false, Type.Missing, Type.Missing);
myExcel.Workbooks.Close();
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myRrange);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myWS);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myWB);
myWS = null;
myWB = null;
myExcel = null;
GC.Collect();
Excel.ApplicationClass myApp=new Excel.ApplicationClass();
系统出错,"0x6dde20e5"指令引用的"0x00000000"内存,该内存不能为"read"
不知道怎解?