private void SetPropertyValue(object model, PropertyInfo property, int rowIndex, List <CellValue> columnTitleCells) { // DisplayName -> columnTitle -> columnIndex object[] attributes = property.GetCustomAttributes(typeof(DisplayNameAttribute), true); string displayName = ((DisplayNameAttribute)attributes[0]).DisplayName; CellValue cell = columnTitleCells.FirstOrDefault(c => c.Value.ToString().Equals(displayName)); if (cell == null) { return; } int colIndex = cell.Col; // 查找对应单元格的数据 object value = CellValueCollection.FirstOrDefault(c => c.Row == rowIndex && c.Col == colIndex).Value; // 格式转换 object propertyValue = null; if (property.PropertyType == typeof(Guid)) { propertyValue = new Guid(value.ToString()); } else { propertyValue = Convert.ChangeType(value, property.PropertyType); } property.SetValue(model, propertyValue, null); }
/// <summary> /// 读取报表标题(通常在[第一行,第一列]) /// </summary> /// <returns>报表标题</returns> public string GetExcelTitle() { if (!HasTitle) { return(string.Empty); } int titleRowIndex = MinRowIndex; // 标题行 return(CellValueCollection.FirstOrDefault(c => c.Row == MinRowIndex && c.Col == MinColIndex).Value.ToString()); }