我是编程新手,现在我正在尝试为我工作的公司编写一个发票制作应用程序。这是我正在努力实现的,目的是让我的任务变得简单。
因此,有一个 excel 文件夹,其中包含项目编号及其各自的净重和毛重,如下所示;
MTS 0001 / 24 / 26
MTS 0002 / 24 / 26
MTS 0003 / 28 / 30
MTS 0008 / 34 / 36
此信息包含在 myworkbook.xlsx、Sheet2 中,因此我在 Sheet2.cs 中实现了一个函数,如下所示;
public bool FindNetWeight(string item, out int weight)
{
for (int i = 0; i < 250; i++)
{
if (Cells[1, i])
{
return int.TryParse(Cells[3, i], out weight);
}
}
weight = 0;
return false;
}
现在,在我的 Form.cs 中,它是我的 Windows 窗体的 cs,我正在尝试使用 FindNetWeight,它告诉我我需要一个字符串参数和一个整数输出,但它不允许我使用该函数并给我以下错误;
An object reference is required for the nonstatic field, method, or property
这可能是什么问题?希望我的描述足够清楚,并且我已经提供了所有必要的信息。
最佳答案
看起来您正在从静态函数调用非静态函数。 您可以将 FindNetWeight 设为静态,也可以创建 Sheet2 的新实例。
简单的方法是让它成为静态的,所以:
public static bool FindNetWeight(string item, out int weight)
{
//yourcode
}
或者,创建 Sheet2 的新实例:
Sheet2 mySheet = new Sheet2();
mySheet.FindNetWeight(blabla)
关于c# - 使用 excel 文件夹作为我的 Windows 窗体的数据库,我无法访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46387702/