2010-11-12 89 views
1

我有一个创建的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++; 
      } 
     } 
    } 
} 
+0

当你说“失败” - ?。会发生什么 – 2010-11-12 07:46:21

+0

我修改的例子一点点这样可以很清楚它的工作原理与POCO对象的列表,但它无法与字符串列表我怎样才能使它与工作String,Int32等列表? – Manuel 2010-11-12 16:26:52

回答

0

下面解析一个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); 
    }