全局访问C#Excel的变量和属性

时间:2022-01-12 00:10:08

I am creating an accounting add in for Microsoft Excel using C#. In this add in, I want to exchange user data between individual forms. The closest thing I got was:

我正在使用C#为Microsoft Excel创建一个会计添加。在此添加中,我想在各个表单之间交换用户数据。我得到的最接近的是:

namespace ExcelAddIn
{
    public partial class ThisAddIn
    {
        public void RefreshExcelData()
        {
        }
    }
}

And...

private void btnUploadTestCases_Click(object sender, EventArgs e)
{
    var addIn = Globals.ThisAddIn;
    addIn.RefreshExcelData();           
}

This works to varying degrees of success, and it only allows me to call methods from that class. It doesn't allow me to access variables or class properties I instantiate within ThisAddIn.cs. What I need to know is how to initialize data (variables, properties, methods, etc.) such that it can be retrieved from any item in the solution, including my forms.

这取决于不同程度的成功,它只允许我从该类调用方法。它不允许我访问我在ThisAddIn.cs中实例化的变量或类属性。我需要知道的是如何初始化数据(变量,属性,方法等),以便可以从解决方案中的任何项目(包括我的表单)中检索它。

And for the love of God, keep it simple. I've spent the better part of a month on MSDN with far too much complications and little success.

为了上帝的爱,保持简单。我在MSDN上度过了一个月的大部分时间,并没有太多的复杂性和成功。

1 个解决方案

#1


0  

First define a field in the addin (lets call it globalString):

首先在插件中定义一个字段(让我们称之为globalString):

namespace ExcelAddIn{       
    public partial class ThisAddIn{
        public static string globalString;
        public void RefreshExcelData(){
        }
    }
}

Then you can access the globalString variable from another class like this:

然后你可以从另一个类访问globalString变量,如下所示:

namespace ExcelAddIn{
   class TestClass {
      public static void SetTheGlobalStringValue(){
         ThisAddIn.globalString = "Global String";
      }
   }
}

#1


0  

First define a field in the addin (lets call it globalString):

首先在插件中定义一个字段(让我们称之为globalString):

namespace ExcelAddIn{       
    public partial class ThisAddIn{
        public static string globalString;
        public void RefreshExcelData(){
        }
    }
}

Then you can access the globalString variable from another class like this:

然后你可以从另一个类访问globalString变量,如下所示:

namespace ExcelAddIn{
   class TestClass {
      public static void SetTheGlobalStringValue(){
         ThisAddIn.globalString = "Global String";
      }
   }
}