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

时间:2018-04-20 09:35:44

标签: c# excel-dna

我有一个简单的POCO“员工”课程,如下所示。

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

我创建了该类的数组。我使用excel dna将此程序集注册为excel。当从excel调用以下方法“GetEmployeeAsync”时,我收到错误“#VALUE!”在每个excel细胞中。如何在ExcelDna中显示?当我将其作为对象[]而不是Poco类型返回时,它可以工作。这是Excel-Dna的限制吗?是否可以通过excel-dna将.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;
}

1 个答案:

答案 0 :(得分:1)

Excel-DNA仅内置支持与Excel C API本机支持的类型匹配的.NET类型。要支持其他数据类型,需要进行一些转换 - 在这种情况下,从List<Employee>object[,]数组。您可以自己进行转换,也可以使用Excel-DNA Registration帮助程序库来注册将在运行时应用的转换。您还可以实现一个泛型转换,它使用反射来公开您的类的所有属性,或类似的东西。但是在盒子里面没有这样的东西&#39;目前。

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