代码如下:string ExcelFile=@"E:\tmp\template.xls";
object oMissiong;
Excel.Workbooks myBooks;
Excel.Workbook  myBook;
Excel.Worksheet mySheet;
Excel.Application myApp=new Excel.ApplicationClass();
myApp.Visible = false;
oMissiong = System.Reflection.Missing.Value;
myBooks= myApp.Workbooks;
myBook = myBooks.Open(ExcelFile,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
mySheet=(Excel.Worksheet)myBook.Sheets[1];if(myBook != null)
myBook.Close(true, ExcelFile, oMissiong);// Quit Excel and clean up.
if(mySheet != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (mySheet);
mySheet = null;
if(myBook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (myBook);
myBook = null;
if(myBooks != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (myBooks);
myBooks = null;
if(myApp != null)
{
myApp.Workbooks.Close();
myApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (myApp);
myApp = null;
}
GC.Collect();