以下是一个完整的关闭excel进程的例子

,我刚用过,没有问题了。
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();

阅读更多
版权声明: https://blog.csdn.net/ycl111/article/details/66021
文章标签: excel null exception
个人分类: office
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭