如何在 .NET 程序集的 Excel-Dna 中显示 POCO 数组

How to display an array of POCO in Excel-Dna from .NET assembly

我得到了一个简单的 POCO "Employee" class,如下所示。

public class Employee
{
    public string Name { get; set; }
    public int Age { get; set; }
    public double Salary { get; set; }
    public string Address { get; set; }
}

我创建了一个 class 的数组。我使用 excel dna 将此程序集注册到 excel。当从 excel 调用以下方法 "GetEmployeeAsync" 时,我收到错误“#VALUE!”在每个 excel 单元格中。我怎样才能在 ExcelDna 中显示它?当我 return 将其作为对象 [] 而不是 Poco 类型时,它就起作用了。 Excel-Dna 有限制吗?是否可以通过 excel-dna 将 return .NET 类型转换为 excel?

ExcelFunction(Description = ".NET function Return Employee Details")]
public static object GetEmployeeAsync(string name)
{
    var empList= new List<Employee>()
    {
        new Employee {Name = name, Age = 30, Salary = 20.0, Address = "London"},
        new Employee {Name = name, Age = 30, Salary = 20.0, Address = "London"},
        new Employee {Name = name, Age = 30, Salary = 20.0, Address = "London"},
        new Employee {Name = name, Age = 30, Salary = 20.0, Address = "London"},
        new Employee {Name = name, Age = 30, Salary = 20.0, Address = "London"},
        new Employee {Name = name, Age = 30, Salary = 20.0, Address = "London"},
        new Employee {Name = name, Age = 30, Salary = 20.0, Address = "London"},
    };

    return empList;
}

Excel-DNA has built-in support only for .NET types that match types natively supported by the Excel C API. To support additional data types there needs to be some conversion - in this case from List<Employee> to an object[,] array. You can either do the conversion yourself, or you can use the Excel-DNA Registration 帮助程序库,用于注册将在运行时应用的转换。您还可以实现一个通用转换,该转换使用反射来公开 class 的所有属性,或类似的东西。但是目前没有这样的东西'in the box'。

对于像这样的一般 Excel-DNA 使用问题,Excel-DNA Google group 是最好的。