如何读取 Excel ListObject 的数据绑定

发布于 2024-09-24 04:10:07 字数 796 浏览 6 评论 0原文

我有一个 VS 2010 Excel 2007 工作簿应用程序。我有一个通过绑定源绑定到对象数据源的 ListObject。我希望能够确定 ListObject 的 ListColumns 集合中的任何给定 ListColumn 对象绑定到我的对象的什么属性。在下面的示例中,我使用列名称来查找绑定到“Field1”属性的列。但是,在我的情况下,列名称可能与属性名称不同。 ListColumn 对象上没有 DataMember、DataPropertyName 或类似属性,如何确定哪个列绑定到哪个属性?

给定下面的类和 ListObject,我希望能够使用以下代码:



return FindColumn(MyDataListObject, "Property1")




Public Class MyData
    Public Property Field1 As String
    Public Property Field2 As Date
End Class

   Public Function FindColumn(ByVal listObject As ListObject, 
ByVal propertyName As String) As ListColumn
        For Each col As ListColumn In listObject.ListColumns
            If col.Name = propertyName Then
                Return col
            End If
        Next
        Return Nothing
    End Function

I have a VS 2010 Excel 2007 Workbook application. I have a ListObject that is bound to an object data source via a binding source. I want to be able to determine what property of my object any given ListColumn obect in my ListObject's ListColumns collection is bound to. In the example below I use the column name to find the column that is bound to the "Field1" property. However, in my situation, the column name can be different from the property Name. There is no DataMember, DataPropertyName, or similar property on a ListColumn object, how do I figure out what column is bound to what property?

Given the class and ListObject below, I want to be able to use the following code:



return FindColumn(MyDataListObject, "Property1")




Public Class MyData
    Public Property Field1 As String
    Public Property Field2 As Date
End Class

   Public Function FindColumn(ByVal listObject As ListObject, 
ByVal propertyName As String) As ListColumn
        For Each col As ListColumn In listObject.ListColumns
            If col.Name = propertyName Then
                Return col
            End If
        Next
        Return Nothing
    End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很糊涂小朋友 2024-10-01 04:10:07

我有类似的情况,我需要 Excel 上的数字绑定列。

不幸的是,VSTO 隐藏了存储信息的字段。

访问此信息的唯一方法是通过System.Reflection。

请看下面的小例子:

    public static string[] GetListObjectMappedColumns(ListObject listObject)
    {
        Tools.ListObject vstoListObject = Globals.Factory.GetVstoObject(listObject);
        FieldInfo fieldInfo = vstoListObject.GetType().GetField("mappedProperties", BindingFlags.Instance | BindingFlags.NonPublic);
        object value = fieldInfo.GetValue(vstoListObject);
        if (value is PropertyDescriptor[])
        {
            List<string> result = new List<string>();
            foreach (PropertyDescriptor propertyDescriptor in (PropertyDescriptor[])value)
                result.Add(propertyDescriptor.Name);
            return result.ToArray();
        }
        return null;
    }

希望对您有帮助!

特奥巴尔多

I have a similar situation and I need figure bound columns on Excel.

Unfortunally, The VSTO hide the fields where the information is stored.

The only way to access this information by System.Reflection.

See below the small example:

    public static string[] GetListObjectMappedColumns(ListObject listObject)
    {
        Tools.ListObject vstoListObject = Globals.Factory.GetVstoObject(listObject);
        FieldInfo fieldInfo = vstoListObject.GetType().GetField("mappedProperties", BindingFlags.Instance | BindingFlags.NonPublic);
        object value = fieldInfo.GetValue(vstoListObject);
        if (value is PropertyDescriptor[])
        {
            List<string> result = new List<string>();
            foreach (PropertyDescriptor propertyDescriptor in (PropertyDescriptor[])value)
                result.Add(propertyDescriptor.Name);
            return result.ToArray();
        }
        return null;
    }

I hope help you !

Teobaldo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文