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