反射读取数组导出

 1.数组与List:
读取数组与列表
数组与List 均有实现IList接口,所以在反射时,获取FieldInfo.FiledType后,可以使用typeof(IList).IsAssignableFrom(fieldType)的方式来判断字段类型是否继承了IList。
如果需要读取IList中值 ,使用FieldInfo.GetValue()获取到值后,转为IList循环遍历即可。

参考原文链接:https://blog.csdn.net/qq_18192161/article/details/105182480

#region ExportAutomaticComparisont
        public void DownAutomaticComparisont(string fileName)
        {


            if (this.excelWorkbook == null)
            {
                excelWorkbook = new HSSFWorkbook();
                doubleStyle = null;
                dateStyle = null;
                this.sheetCount += 1;
                currentSheet = excelWorkbook.CreateSheet("Sheet" + this.sheetCount);
                CreateHeader(currentSheet);
                int rowCount = currentSheet.PhysicalNumberOfRows;
                var listType = dataSource.GetType();
                var objType = listType.GetGenericArguments()[0];
                var count = Convert.ToInt32(listType.GetProperty("Count").GetValue(dataSource, null));
                var items = listType.GetProperty("Item");

                if (objType.IsClass && objType != typeof(string))
                {
                    for (int i = 0; i < count; i++)
                    {
                        IRow newRow = this.currentSheet.CreateRow(rowCount);
                        if (i != 0 && i % this.maxRowCount == 0)
                        {
                            sheetCount++;
                            rowCount = 1;
                            currentSheet = excelWorkbook.CreateSheet("Sheet" + sheetCount);
                            CreateHeader(currentSheet);
                            rowCount = currentSheet.PhysicalNumberOfRows;
                        }
                        //获取列表集合中的对象
                        var obj = items.GetValue(dataSource, new object[] { i });
                        if (obj is JsonObject)
                        {
                            var json = obj as JsonObject;
                            int cellIndex = 0;
                            foreach (var item in this.ColumnNameList)
                            {
                                InsertCellValue(json.GetValue(item.Key), newRow, cellIndex, true);
                                cellIndex++;
                            }
                        }
                        else if (obj is ExpandoObject)
                        {
                            var dataItem = obj as ExpandoObject;
                            var dnmObj = (IDictionary<string, object>)dataItem;
                            int cellIndex = 0;
                            foreach (var item in this.ColumnNameList)
                            {
                                if (dnmObj.ContainsKey(item.Key))
                                {
                                    InsertCellValue(dnmObj[item.Key], newRow, cellIndex, true);
                                }
                                else
                                {
                                    InsertCellValue("", newRow, cellIndex, true);
                                }
                                cellIndex++;
                            }
                        }
                        else
                        {
                            var properties = obj.GetType().GetProperties();

                            int cellIndex = 0;
                            foreach (var item in this.ColumnNameList)
                            {
                                //取子
                                if (item.Key.IndexOf('.') != -1)
                                {
                                    object value = null;

                                    string propertyName = item.Key.Split('.')[0].Split('_')[0];
                                    int j = Convert.ToInt32(item.Key.Split('.')[0].Split('_')[1]);
                                    string key = item.Key.Split('.')[1];
                                    var fieldInfo = obj.GetType().GetProperty(propertyName).GetValue(obj);
                                    var ar =(List<DateRangeSalesEntity>)fieldInfo;
                                    
                                  
                                    if (ar!=null && j< ar.Count() && ar[j]!=null)
                                    {
                                        value = ar[j].GetType().GetProperty(key).GetValue(ar[j]);

                                    }
                                    if (value != null)
                                    {
                                        InsertCellValue(value, newRow, cellIndex, true);
                                    }
                                }
                                else
                                {
                                    var ps = properties.FirstOrDefault(p => p.Name == item.Key);
                                    if (ps != null)
                                    {
                                        InsertCellValue(ps.GetValue(obj, null), newRow, cellIndex, true);
                                    }
                                }

                                cellIndex++;
                            }
                        }

                        rowCount++;
                    }
                }
                else if (objType.IsPrimitive || objType == (typeof(string)))
                {
                    IRow newRow = currentSheet.CreateRow(rowCount);
                    for (int i = 0; i < count; i++)
                    {
                        InsertCellValue(items.GetValue(dataSource, new object[] { i }), newRow, i, true);
                    }
                }
                else
                {
                    throw new BusinessException("Does not identify the generic type");
                }
            }
            MemoryStream ms = new MemoryStream();
            this.excelWorkbook.Write(ms);
            DownLoad(fileName, ms);
        }
        #endregion
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葩熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值