public static List <ExcelCell> ToList <T>(T entity, bool isHeader = true) where T : class { PropertyInfo[] infoList = General.GetPropertyInfo(typeof(T)); List <ExcelCell> cells = new List <ExcelCell>(); ExcelCell cell = null; int ascNumber = 65; IList entityList = (entity is IList ? entity as IList : new List <T> { entity }); uint rowIndex = 1; if (isHeader) { rowIndex = 2; foreach (PropertyInfo info in infoList) { TypeCode tc = System.Type.GetTypeCode(info.PropertyType); cell = new ExcelCell(); cell.Name = ascNumber.ConvertFromASCII(); cell.RowIndex = 1; cell.Type = CellValues.String; cell.Value = info.Name; cells.Add(cell); ascNumber++; } } for (int i = 0; i < entityList.Count; i++) { var en = entityList[i]; ascNumber = 65; foreach (PropertyInfo info in infoList) { TypeCode tc = System.Type.GetTypeCode(info.PropertyType); object infoValue = info.GetValue(en, null); infoValue = (infoValue == null ? string.Empty : infoValue); cell = new ExcelCell(); cell.Name = ascNumber.ConvertFromASCII(); cell.RowIndex = rowIndex; cell.Type = CellValues.String; cell.Value = (tc == TypeCode.DateTime ? ((DateTime)infoValue).ToString("yyyy-MM-dd HH:mm") : infoValue.ToString()); cells.Add(cell); ascNumber++; } rowIndex++; } return(cells); }
public bool CreateExcel <T>(T entity, string sheetName) where T : class { this.Cells = ExcelCell.ToList(entity); return(CreatExcel(sheetName)); }