CS0116 命名空间不能直接包含字段或方法等成员 C# Microsoft.Office.Interop.Excel
CS0116 A namespace cannot directly contain members such as field or methods C# Microsoft.Office.Interop.Excel
正在使用 C#
处理 Office Interop Objects,我遇到了一个错误,这里是出现错误的主要部分:
using Excel = Microsoft.Office.Interop.Excel;
...
static void DisplayInExcel (IEnumerable<Account> accounts)
{
var excelApp = new Excel.Application();
excelApp.Visible = true;
excelApp.Workbooks.Add();
Excel._Worksheet workSheet = (Excel.Worksheet).excelApp.ActiveSheet;
}
您可能错过了代码中的 class 声明。现在你有这样的东西:
namespace MyNamespace
{
static void DisplayInExcel (IEnumerable<Account> accounts)
{
var excelApp = new Excel.Application();
excelApp.Visible = true;
excelApp.Workbooks.Add();
Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
}
}
你需要用 class
包装你的函数
namespace MyNamespace
{
public class MyClass
{
public static void DisplayInExcel (IEnumerable<Account> accounts)
{
var excelApp = new Excel.Application();
excelApp.Visible = true;
excelApp.Workbooks.Add();
Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
}
}
}
然后像这样访问它
MyClass.DisplayInExcel(accounts)
正在使用 C#
处理 Office Interop Objects,我遇到了一个错误,这里是出现错误的主要部分:
using Excel = Microsoft.Office.Interop.Excel;
...
static void DisplayInExcel (IEnumerable<Account> accounts)
{
var excelApp = new Excel.Application();
excelApp.Visible = true;
excelApp.Workbooks.Add();
Excel._Worksheet workSheet = (Excel.Worksheet).excelApp.ActiveSheet;
}
您可能错过了代码中的 class 声明。现在你有这样的东西:
namespace MyNamespace
{
static void DisplayInExcel (IEnumerable<Account> accounts)
{
var excelApp = new Excel.Application();
excelApp.Visible = true;
excelApp.Workbooks.Add();
Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
}
}
你需要用 class
包装你的函数namespace MyNamespace
{
public class MyClass
{
public static void DisplayInExcel (IEnumerable<Account> accounts)
{
var excelApp = new Excel.Application();
excelApp.Visible = true;
excelApp.Workbooks.Add();
Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
}
}
}
然后像这样访问它
MyClass.DisplayInExcel(accounts)