我无法通过Excel考试.应用程序作为方法的对象

本文关键字:方法 对象 应用程序 考试 Excel | 更新日期: 2023-09-27 17:51:03

非静态方法ExcelParser.Program.releaseObject(object)需要对象引用。我不知道为什么我得到这个错误。我无法通过Excel考试。应用程序,Excel。Excel工作簿。工作表作为一个对象调用releaseObject方法。下面是代码:

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();
        }
    } 
}
}

我无法通过Excel考试.应用程序作为方法的对象

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

改变:

private void releaseObject(object obj)

private static void releaseObject(object obj)