需要帮助获取readxml的简单方法

时间:2022-08-05 01:50:18

Need help on getting a simple method to readxml. Here are the steps that I have taken:

需要帮助获取readxml的简单方法。以下是我采取的步骤:

  1. Added reference from Microsoft Excel 12.0 Object Library COM.
  2. 添加了Microsoft Excel 12.0对象库COM的参考。

  3. Copied code exactly from another source http://csharp.net-informations.com/excel/csharp-read-excel.htm
  4. 完全从其他来源复制的代码http://csharp.net-informations.com/excel/csharp-read-excel.htm

  5. The only difference that I would require this to be a method and Not an onclick event.
  6. 唯一的区别是我需要这个方法而不是onclick事件。

  7. The error I'm getting is Error 10 An object reference is required for the non-static field, method, or property on code releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp);
  8. 我得到的错误是错误10代码releaseObject(xlWorkSheet)上的非静态字段,方法或属性需要对象引用; releaseObject(xlWorkBook); releaseObject(xlApp);

What are the steps required?

需要哪些步骤?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace projectName
{
class frmReadXml
{

    public static void executeRead()
    {

        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet = new Excel.Worksheet();
        Excel.Range range;

        string str;
        int rCnt = 0;
        int cCnt = 0;



        xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 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);

        range = xlWorkSheet.UsedRange;

        for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
        {
            for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
            {
                str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                MessageBox.Show(str);
            }
        }

        xlWorkBook.Close(true, null, null);
        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;
            MessageBox.Show("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    } 

}

}

2 个解决方案

#1


0  

make releaseObject as static method

将releaseObject作为静态方法

private static void releaseObject(object obj)

#2


0  

Because your function:

因为你的功能:

public static void executeRead()

is declared as static, it only has access to other functions that are also defined as static. Either remove the static on this function, or add it to the others. Since none of these seem to access any class members of the frmReadExcel, I suggest you make them all static.

声明为静态,它只能访问其他也定义为静态的函数。删除此功能上的静态,或将其添加到其他功能。由于这些似乎都没有访问frmReadExcel的任何类成员,我建议你将它们全部静态化。

#1


0  

make releaseObject as static method

将releaseObject作为静态方法

private static void releaseObject(object obj)

#2


0  

Because your function:

因为你的功能:

public static void executeRead()

is declared as static, it only has access to other functions that are also defined as static. Either remove the static on this function, or add it to the others. Since none of these seem to access any class members of the frmReadExcel, I suggest you make them all static.

声明为静态,它只能访问其他也定义为静态的函数。删除此功能上的静态,或将其添加到其他功能。由于这些似乎都没有访问frmReadExcel的任何类成员,我建议你将它们全部静态化。