简体   繁体   English

Visual Studio Windows 窗体 C# Interop.Excel - Excel 类对象

[英]Visual Studio Windows Forms C# Interop.Excel - Excel Class Object

I have created a C# Windows Form project in Visual Studio and I am trying to work with an Excel workbook via interop.excel .我在 Visual Studio 中创建了一个 C# Windows 窗体项目,我正在尝试通过interop.excel使用 Excel 工作簿。 I have created a custom "excel class" and created an object of it in my Form1 .我创建了一个自定义的“excel 类”并在我的Form1创建了它的一个对象。
What I am struggling with is whether it is possible to open an excel workbook via a button press, ie create the class object from a button press, and then be able to use that object in other button presses.我正在努力解决的是是否可以通过按下按钮打开 Excel 工作簿,即通过按下按钮创建类对象,然后能够在其他按钮按下时使用该对象。

Two versions of code are shown below.两个版本的代码如下所示。 One works,one does not.一个有效,一个无效。 The one that works just opens the Excel workbook when the program is launched.一个有效的只是在程序启动时打开 Excel 工作簿。 The other attempts to use a button press on the form to open the workbook after the program is launched.另一个尝试在程序启动后使用表单上的按钮打开工作簿。 In the code that does not work, the object does not exist in the current context .在不起作用的代码does not exist in the current context该对象。
Any help on how to make the button press code work is most appreciated!非常感谢有关如何使按钮按下代码工作的任何帮助!

This code works:此代码有效:

namespace XLtest1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    ExcelClass ex  = new ExcelClass(@"C:\path\TestBook.xlsx", 1);

    private void ReadCell_Click(object sender, EventArgs e)
    {
        ex.ReadCell();
    }

... ...

This code does not:此代码不会:

namespace XLtest1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void OpenFile()
    {
        ExcelClass ex  = new ExcelClass(@"C:\path\TestBook.xlsx", 1);
    }

    private void OpenWorkbook_Click(object sender, EventArgs e)
    {
      OpenFile();
    }

    private void ReadCell_Click(object sender, EventArgs e)
    {
        ex.ReadCell(); // "ex" does not exist in the current context 
    }

... ...

I think you are asking to be able to reuse the code on another button press?我认为您要求能够在另一个按钮按下时重用代码? If so would it be an option for you to create a function with the code needed and just call that function inside each button press?如果是这样,您是否可以选择使用所需的代码创建一个函数,然后在每次按下按钮时调用该函数?

You are using a local variable inside ReadCell which has no knowledge of ex.您在 ReadCell 中使用了一个不了解 ex 的局部变量。

You could create a variable你可以创建一个变量

public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
}
private ExcelClass ex = null; //create your member field here

public void OpenFile()
{
    ex = new ExcelClass(@"C:\path\TestBook.xlsx", 1);
}

private void OpenWorkbook_Click(object sender, EventArgs e)
{
  OpenFile();
}

private void ReadCell_Click(object sender, EventArgs e)
{
    if (ex!= null) {
        ex.ReadCell();
    } else {
        //do nothing, or inform user they have to press other button to open the file
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM