如何导入/解析采集数据?

发布于 2024-10-01 17:45:48 字数 2231 浏览 2 评论 0原文

我有一个创建 Excel 文件的项目,我想添加一些导入功能。简而言之,我想让用户能够编写以下代码:

worksheet.Cell(1, 1).Value = collectionObject;

(其中 collectionObject 实现 IEnumerable)

我如何解析 IEnumerable任何类型并提取每个项目的属性和字段的值?

这是我失败的尝试:

class Program
{
    static void Main(string[] args)
    {
        // It fails with a list of strings
        var listOfStrings = new List<String>();
        listOfStrings.Add("House");
        listOfStrings.Add("Car");
        new ParseCollections().ParseCollection(listOfStrings, 1, 1);
        // Get error "Parameter count mismatch." when calling info.GetValue(m, null).ToString()
        // The property is an array "Chars". 
        // I tried filtering the property with "info as IEnumerable == null" but it doesn't catch it.
        // How can I filter collection properties here?


        // It works with a list of POCO
        var listOfPOCO = new List<Person>();
        listOfPOCO.Add(new Person() { Name = "John", Age = 30 });
        listOfPOCO.Add(new Person() { Name = "Jane", Age = 25 });
        new ParseCollections().ParseCollection(listOfPOCO, 1, 1);
    }

    class Person
    {
        public String Name { get; set; }
        public Int32 Age { get; set; }
    }
}
class ParseCollections
{
    public void ParseCollection(Object collectionObject, Int32 initialRow, Int32 initialColumn)
    {
        var asEnumerable = collectionObject as IEnumerable;
        if (asEnumerable != null)
        {
            var ro = initialRow;
            foreach (var m in asEnumerable)
            {
                var co = initialColumn;
                var fieldInfo = m.GetType().GetFields();
                foreach (var info in fieldInfo)
                {
                    Console.WriteLine("Cell({0}, {1}) = {2}", ro, co, info.GetValue(m).ToString());
                    co++;
                }
                var propertyInfo = m.GetType().GetProperties();
                foreach (var info in propertyInfo)
                {
                    Console.WriteLine("Cell({0}, {1}) = {2}", ro, co, info.GetValue(m, null).ToString());
                    co++;
                }
                ro++;
            }
        } 
    }
}

I have a project that creates Excel files and I'd like to add some import functionality. In a nutshell I'd like to give users the ability to code the following:

worksheet.Cell(1, 1).Value = collectionObject;

(Where collectionObject implements IEnumerable)

How can I parse an IEnumerable of any type and extract the values of each item's properties and fields?

This is my failed attempt:

class Program
{
    static void Main(string[] args)
    {
        // It fails with a list of strings
        var listOfStrings = new List<String>();
        listOfStrings.Add("House");
        listOfStrings.Add("Car");
        new ParseCollections().ParseCollection(listOfStrings, 1, 1);
        // Get error "Parameter count mismatch." when calling info.GetValue(m, null).ToString()
        // The property is an array "Chars". 
        // I tried filtering the property with "info as IEnumerable == null" but it doesn't catch it.
        // How can I filter collection properties here?


        // It works with a list of POCO
        var listOfPOCO = new List<Person>();
        listOfPOCO.Add(new Person() { Name = "John", Age = 30 });
        listOfPOCO.Add(new Person() { Name = "Jane", Age = 25 });
        new ParseCollections().ParseCollection(listOfPOCO, 1, 1);
    }

    class Person
    {
        public String Name { get; set; }
        public Int32 Age { get; set; }
    }
}
class ParseCollections
{
    public void ParseCollection(Object collectionObject, Int32 initialRow, Int32 initialColumn)
    {
        var asEnumerable = collectionObject as IEnumerable;
        if (asEnumerable != null)
        {
            var ro = initialRow;
            foreach (var m in asEnumerable)
            {
                var co = initialColumn;
                var fieldInfo = m.GetType().GetFields();
                foreach (var info in fieldInfo)
                {
                    Console.WriteLine("Cell({0}, {1}) = {2}", ro, co, info.GetValue(m).ToString());
                    co++;
                }
                var propertyInfo = m.GetType().GetProperties();
                foreach (var info in propertyInfo)
                {
                    Console.WriteLine("Cell({0}, {1}) = {2}", ro, co, info.GetValue(m, null).ToString());
                    co++;
                }
                ro++;
            }
        } 
    }
}

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

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

发布评论

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

评论(1

拥抱我好吗 2024-10-08 17:45:48

下面解析一个 IEnumerable 对象,但这似乎是一种人为的方式,只是乞求用户传递一个会破坏函数的 IEnumerable :(

        public void ParseCollection(Object collectionObject, Int32 initialRow, Int32 initialColumn)
    {
        var asEnumerable = collectionObject as IEnumerable;
        if (asEnumerable != null)
        {
            var ro = initialRow;
            foreach (var m in asEnumerable)
            {
                var co = initialColumn;

                if (m.GetType().IsPrimitive || m.GetType() == typeof(String) || m.GetType() == typeof(DateTime))
                {
                    SetValue(m, ro, co);
                }
                else if (m.GetType().IsArray )
                {
                    dynamic arr = m;
                    foreach(var item in arr)
                    {
                        SetValue(item, ro, co);
                        co++;
                    }
                }
                else if ((m as DataRow) != null)
                {
                    foreach (var item in (m as DataRow).ItemArray)
                    {
                        SetValue(item, ro, co);
                        co++;
                    }
                }
                else
                {
                    var fieldInfo = m.GetType().GetFields();
                    foreach (var info in fieldInfo)
                    {
                        SetValue(info.GetValue(m), ro, co);
                        co++;
                    }
                    var propertyInfo = m.GetType().GetProperties();
                    foreach (var info in propertyInfo)
                    {
                        if ((info as IEnumerable) == null)
                            SetValue(info.GetValue(m, null), ro, co);
                        co++;
                    }
                }
                ro++;
            }
        } 
    }
    private static void SetValue(object objWithValue, int ro, int co)
    {
        String str = String.Empty;
        if (objWithValue != null)
            str = objWithValue.ToString();

        Console.WriteLine("Cell({0}, {1}) = {2}", ro, co, str);
    }

The following parses an IEnumerable object but it seems way to contrived and just begging for a user to pass an IEnumerable that will break the function :(

        public void ParseCollection(Object collectionObject, Int32 initialRow, Int32 initialColumn)
    {
        var asEnumerable = collectionObject as IEnumerable;
        if (asEnumerable != null)
        {
            var ro = initialRow;
            foreach (var m in asEnumerable)
            {
                var co = initialColumn;

                if (m.GetType().IsPrimitive || m.GetType() == typeof(String) || m.GetType() == typeof(DateTime))
                {
                    SetValue(m, ro, co);
                }
                else if (m.GetType().IsArray )
                {
                    dynamic arr = m;
                    foreach(var item in arr)
                    {
                        SetValue(item, ro, co);
                        co++;
                    }
                }
                else if ((m as DataRow) != null)
                {
                    foreach (var item in (m as DataRow).ItemArray)
                    {
                        SetValue(item, ro, co);
                        co++;
                    }
                }
                else
                {
                    var fieldInfo = m.GetType().GetFields();
                    foreach (var info in fieldInfo)
                    {
                        SetValue(info.GetValue(m), ro, co);
                        co++;
                    }
                    var propertyInfo = m.GetType().GetProperties();
                    foreach (var info in propertyInfo)
                    {
                        if ((info as IEnumerable) == null)
                            SetValue(info.GetValue(m, null), ro, co);
                        co++;
                    }
                }
                ro++;
            }
        } 
    }
    private static void SetValue(object objWithValue, int ro, int co)
    {
        String str = String.Empty;
        if (objWithValue != null)
            str = objWithValue.ToString();

        Console.WriteLine("Cell({0}, {1}) = {2}", ro, co, str);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文