2015-05-29 113 views
0

非静态方法ExcelParser.Program.releaseObject(object)需要对象引用。我不知道为什么我得到这个错误。我无法设法将Excel.Application,Excel.Workbook,Excel.Worksheet作为对象传递给releaseObject方法。 下面是代码:我无法将Excel.Application作为对象传递给方法

using System; 
using System.Collections.Generic; 

using System.Linq; 
using System.Text; 
using Excel = Microsoft.Office.Interop.Excel; 


namespace ExcelParser 
{ 
class Program 
{ 

    static void Main(string[] args) 
    { 
     Excel.Application xlApp ; 
     Excel.Workbook xlWorkBook ; 
     Excel.Worksheet xlWorkSheet ; 
     object misValue = System.Reflection.Missing.Value; 
     xlApp = new Excel.Application(); 
     xlWorkBook = xlApp.Workbooks.Open("C:/Users/mmm/hello.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); 
     xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
     Console.WriteLine(xlWorkSheet.get_Range("A1","A1").Value2.ToString()); 
     xlWorkBook.Close(true, misValue, misValue); 
     xlApp.Quit(); 
     releaseObject(xlWorkSheet); 
     releaseObject(xlWorkBook); 
     releaseObject(xlApp); 
    } 

    private void releaseObject(object obj) 
    { 
     try 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); 
      obj = null; 
     } 

     catch (Exception ex) 
     { 
      obj = null; 
      Console.WriteLine("Unable to release the Object " + ex.ToString()); 
     } 

     finally 
     { 
      GC.Collect(); 
     } 
    } 
} 

} 

回答

1

不能调用从静态方法非静态方法。

变化:

private void releaseObject(object obj) 

private static void releaseObject(object obj) 
相关问题