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