using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; namespace FWindSoft.Revit.Table { /// /// TSheet数据标识属性 /// [AttributeUsage(AttributeTargets.Property,AllowMultiple=false)] public sealed class TSheetFileAttribute : Attribute { public TSheetFileAttribute(string fileName) { this.FileName = fileName; } /// /// 字段名称 /// public string FileName { get; private set; } } #region 表格使用类 /// /// 行设置 /// public class WordSetting { /// /// 行高 /// public double RowHeight { get; set; } /// /// 字高 /// public double WordHeight { get; set; } } /// /// 列设置 /// public class ColumnSetting { public ColumnSetting() { } public ColumnSetting(string content) { Name = content; Display = content; TextAlignment=TextAlignment.Middle; IsShow = true; } /// /// 列排序索引 /// public int Index { get; set; } /// /// 列显示内容 /// public string Name { get; set; } /// /// 列头显示名称 /// public string Display { get; set; } /// /// 列宽 /// public double ColomnWidth { get; set; } /// /// 文本对齐 /// public TextAlignment TextAlignment { get; set; } /// /// 是否显示 /// public bool IsShow { get; set; } public void SetValue(ColumnSetting setting) { Index = setting.Index; Display = setting.Display; ColomnWidth = setting.ColomnWidth; TextAlignment = setting.TextAlignment; IsShow = setting.IsShow; } } /// /// 内容排版 /// public enum ContentArragne { [Description("横向排列")] Landscape, [Description("纵向排列")] Portrait } /// /// 文字对齐方式 /// public enum TextAlignment { [Description("左对齐")] Left, [Description("居中")] Middle, [Description("右对齐")] Right, } #endregion /// /// 表格设置 /// public class TSheet { /// /// 构造函数不能私有化,是因为需要序列化和反序列化 /// public TSheet() { this.FontString = "宋体"; this.IsBottom = false; this.IsShowTitleName = false; this.TitleName = string.Format("表格名称"); this.TitleWordSetting = new WordSetting() { RowHeight = 8, WordHeight = 5 }; this.ColumnSettings = new List(); this.AutoFit = false; this.SheetHeaderWordSetting = new WordSetting() { RowHeight = 5, WordHeight = 3.5 }; this.SheetRowWordSetting = new WordSetting() { RowHeight = 5, WordHeight = 3.5 }; this.IsUsePagination = false; this.Arrange = ContentArragne.Landscape; this.RowCountPrePage = 5; } #region 属性控制维护 /// /// 表格字体 /// public string FontString { get; set; } /// /// 表头是否置底 /// public bool IsBottom{get;set;} /* * 为了分层明确,也为了更适合通用型,属于mode层的类尽量做到能不做属性通知就不做属性通知 */ #region 标题内容 #endregion #region 标题内容 /// /// 是否显示标题名字 /// public bool IsShowTitleName { get; set; } /// /// 标题名称 /// public string TitleName { get; set; } /// /// 标题文字设置 /// public WordSetting TitleWordSetting { get; set; } #endregion #region 列设置 /// /// 列设置 /// public List ColumnSettings { get;private set; } /// /// 列宽自适应 /// public bool AutoFit { get; set; } #endregion #region 表头文字设置 /// /// 表头文字设置 /// public WordSetting SheetHeaderWordSetting { get; set; } #endregion #region 表格文字设置 /// /// 表格文字设置 /// public WordSetting SheetRowWordSetting { get; set; } #endregion #region 分页设置 /// /// 是否使用分页 /// public bool IsUsePagination { get; set; } /// /// 每页行数 /// public int RowCountPrePage { get; set; } /// /// 内容排版格式 /// public ContentArragne Arrange { get; set; } #endregion #region 控制序号 /// /// 是否动态生成序号 /// public bool AutoBuildNo { get; set; } /// /// 序号列名称 /// public string NoColumnName { get; set; } #endregion #endregion /// /// 添加列信息 /// /// public void AddColumns(ColumnSetting column) { if (column == null) return; this.ColumnSettings.Add(column); } public void Draw(ITSheetContext context) { context.Draw(this); } #region 取数据 public static List> GetSheetData(object o) { List> result = new List>(); ITSheetData getData = o as ITSheetData; if (getData != null) { result.AddRange(getData.CreateSheetData()); return result; } Dictionary dic = new Dictionary(); Type type = o.GetType(); var properties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var property in properties) { try { if ((property.PropertyType.IsClass && property.PropertyType != typeof(string)) || property.PropertyType.IsAbstract) continue; TSheetFileAttribute[] attrs = property.GetCustomAttributes(typeof(TSheetFileAttribute), true) as TSheetFileAttribute[]; if (attrs.Any()) { dic[attrs[0].FileName] = property.GetValue(o); } } catch (Exception) { } } result.Add(dic); return result; } /// /// 单一类型集合,如果Object类型不一致,请手动使用GetSheetData(object o) /// /// /// public static List> GetSheetData(List objects) { List> dic = new List>(); if (!objects.Any()) return dic; Type type = objects[0].GetType(); var properties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); Dictionary pros = new Dictionary(); foreach (var property in properties) { try { if ((property.PropertyType.IsClass&&property.PropertyType!=typeof(string))|| property.PropertyType.IsAbstract) continue; TSheetFileAttribute[] attrs = property.GetCustomAttributes(typeof(TSheetFileAttribute), true) as TSheetFileAttribute[]; if (attrs.Any()) { pros[attrs[0].FileName] = property; } } catch (Exception) { } } for (int i = 0; i < objects.Count; i++) { object tempObject = objects[i]; ITSheetData iTSheet = tempObject as ITSheetData; if (iTSheet != null) { dic.AddRange(iTSheet.CreateSheetData()); continue; } Dictionary tempDicObject = new Dictionary(); foreach (var propertyInfo in pros) { tempDicObject.Add(propertyInfo.Key, propertyInfo.Value.GetValue(tempObject)); } dic.Add(tempDicObject); } return dic; } public static int GetRowCount(object o) { //其他情况都是1,继承自ITSheetData时自动获取 //ITSheetData tData = o as ITSheetData; return GetSheetData(o).Count; } #endregion } public class TCell { public TCell() { this.RowSpan = 1; this.ColumnSpan = 1; } /// /// 列索引 /// public int ColumnIndex { get; set; } /// ///行索引 /// public int RowIndex { get; set; } /// /// 行跨度 /// public int RowSpan { get; set; } /// /// 列跨度 /// public int ColumnSpan { get; set; } /// /// 单元格值 /// public object Value { get; set; } /// /// 标签扩展 /// public object Tag { get; set; } } /// /// 绘图逻辑 /// public interface ITSheetContext { void Draw(TSheet sheet); } /// /// 获取表格数据 /// public interface ITSheetData { /// /// 创建表格数据 /// /// List> CreateSheetData(); } }