TSheet.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace FWindSoft.Revit.Table
  7. {
  8. /// <summary>
  9. /// TSheet数据标识属性
  10. /// </summary>
  11. [AttributeUsage(AttributeTargets.Property,AllowMultiple=false)]
  12. public sealed class TSheetFileAttribute : Attribute
  13. {
  14. public TSheetFileAttribute(string fileName)
  15. {
  16. this.FileName = fileName;
  17. }
  18. /// <summary>
  19. /// 字段名称
  20. /// </summary>
  21. public string FileName { get; private set; }
  22. }
  23. #region 表格使用类
  24. /// <summary>
  25. /// 行设置
  26. /// </summary>
  27. public class WordSetting
  28. {
  29. /// <summary>
  30. /// 行高
  31. /// </summary>
  32. public double RowHeight { get; set; }
  33. /// <summary>
  34. /// 字高
  35. /// </summary>
  36. public double WordHeight { get; set; }
  37. }
  38. /// <summary>
  39. /// 列设置
  40. /// </summary>
  41. public class ColumnSetting
  42. {
  43. public ColumnSetting()
  44. {
  45. }
  46. public ColumnSetting(string content)
  47. {
  48. Name = content;
  49. Display = content;
  50. TextAlignment=TextAlignment.Middle;
  51. IsShow = true;
  52. }
  53. /// <summary>
  54. /// 列排序索引
  55. /// </summary>
  56. public int Index { get; set; }
  57. /// <summary>
  58. /// 列显示内容
  59. /// </summary>
  60. public string Name { get; set; }
  61. /// <summary>
  62. /// 列头显示名称
  63. /// </summary>
  64. public string Display { get; set; }
  65. /// <summary>
  66. /// 列宽
  67. /// </summary>
  68. public double ColomnWidth { get; set; }
  69. /// <summary>
  70. /// 文本对齐
  71. /// </summary>
  72. public TextAlignment TextAlignment { get; set; }
  73. /// <summary>
  74. /// 是否显示
  75. /// </summary>
  76. public bool IsShow { get; set; }
  77. public void SetValue(ColumnSetting setting)
  78. {
  79. Index = setting.Index;
  80. Display = setting.Display;
  81. ColomnWidth = setting.ColomnWidth;
  82. TextAlignment = setting.TextAlignment;
  83. IsShow = setting.IsShow;
  84. }
  85. }
  86. /// <summary>
  87. /// 内容排版
  88. /// </summary>
  89. public enum ContentArragne
  90. {
  91. [Description("横向排列")]
  92. Landscape,
  93. [Description("纵向排列")]
  94. Portrait
  95. }
  96. /// <summary>
  97. /// 文字对齐方式
  98. /// </summary>
  99. public enum TextAlignment
  100. {
  101. [Description("左对齐")]
  102. Left,
  103. [Description("居中")]
  104. Middle,
  105. [Description("右对齐")]
  106. Right,
  107. }
  108. #endregion
  109. /// <summary>
  110. /// 表格设置
  111. /// </summary>
  112. public class TSheet
  113. {
  114. /// <summary>
  115. /// 构造函数不能私有化,是因为需要序列化和反序列化
  116. /// </summary>
  117. public TSheet()
  118. {
  119. this.FontString = "宋体";
  120. this.IsBottom = false;
  121. this.IsShowTitleName = false;
  122. this.TitleName = string.Format("表格名称");
  123. this.TitleWordSetting = new WordSetting() { RowHeight = 8, WordHeight = 5 };
  124. this.ColumnSettings = new List<ColumnSetting>();
  125. this.AutoFit = false;
  126. this.SheetHeaderWordSetting = new WordSetting() { RowHeight = 5, WordHeight = 3.5 };
  127. this.SheetRowWordSetting = new WordSetting() { RowHeight = 5, WordHeight = 3.5 };
  128. this.IsUsePagination = false;
  129. this.Arrange = ContentArragne.Landscape;
  130. this.RowCountPrePage = 5;
  131. }
  132. #region 属性控制维护
  133. /// <summary>
  134. /// 表格字体
  135. /// </summary>
  136. public string FontString { get; set; }
  137. /// <summary>
  138. /// 表头是否置底
  139. /// </summary>
  140. public bool IsBottom{get;set;}
  141. /*
  142. * 为了分层明确,也为了更适合通用型,属于mode层的类尽量做到能不做属性通知就不做属性通知
  143. */
  144. #region 标题内容
  145. #endregion
  146. #region 标题内容
  147. /// <summary>
  148. /// 是否显示标题名字
  149. /// </summary>
  150. public bool IsShowTitleName { get; set; }
  151. /// <summary>
  152. /// 标题名称
  153. /// </summary>
  154. public string TitleName { get; set; }
  155. /// <summary>
  156. /// 标题文字设置
  157. /// </summary>
  158. public WordSetting TitleWordSetting { get; set; }
  159. #endregion
  160. #region 列设置
  161. /// <summary>
  162. /// 列设置
  163. /// </summary>
  164. public List<ColumnSetting> ColumnSettings { get;private set; }
  165. /// <summary>
  166. /// 列宽自适应
  167. /// </summary>
  168. public bool AutoFit { get; set; }
  169. #endregion
  170. #region 表头文字设置
  171. /// <summary>
  172. /// 表头文字设置
  173. /// </summary>
  174. public WordSetting SheetHeaderWordSetting { get; set; }
  175. #endregion
  176. #region 表格文字设置
  177. /// <summary>
  178. /// 表格文字设置
  179. /// </summary>
  180. public WordSetting SheetRowWordSetting { get; set; }
  181. #endregion
  182. #region 分页设置
  183. /// <summary>
  184. /// 是否使用分页
  185. /// </summary>
  186. public bool IsUsePagination { get; set; }
  187. /// <summary>
  188. /// 每页行数
  189. /// </summary>
  190. public int RowCountPrePage { get; set; }
  191. /// <summary>
  192. /// 内容排版格式
  193. /// </summary>
  194. public ContentArragne Arrange { get; set; }
  195. #endregion
  196. #region 控制序号
  197. /// <summary>
  198. /// 是否动态生成序号
  199. /// </summary>
  200. public bool AutoBuildNo { get; set; }
  201. /// <summary>
  202. /// 序号列名称
  203. /// </summary>
  204. public string NoColumnName { get; set; }
  205. #endregion
  206. #endregion
  207. /// <summary>
  208. /// 添加列信息
  209. /// </summary>
  210. /// <param name="column"></param>
  211. public void AddColumns(ColumnSetting column)
  212. {
  213. if (column == null)
  214. return;
  215. this.ColumnSettings.Add(column);
  216. }
  217. public void Draw(ITSheetContext context)
  218. {
  219. context.Draw(this);
  220. }
  221. #region 取数据
  222. public static List<Dictionary<string, object>> GetSheetData(object o)
  223. {
  224. List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
  225. ITSheetData getData = o as ITSheetData;
  226. if (getData != null)
  227. {
  228. result.AddRange(getData.CreateSheetData());
  229. return result;
  230. }
  231. Dictionary<string, object> dic = new Dictionary<string, object>();
  232. Type type = o.GetType();
  233. var properties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  234. foreach (var property in properties)
  235. {
  236. try
  237. {
  238. if ((property.PropertyType.IsClass && property.PropertyType != typeof(string)) || property.PropertyType.IsAbstract)
  239. continue;
  240. TSheetFileAttribute[] attrs =
  241. property.GetCustomAttributes(typeof(TSheetFileAttribute), true) as TSheetFileAttribute[];
  242. if (attrs.Any())
  243. {
  244. dic[attrs[0].FileName] = property.GetValue(o);
  245. }
  246. }
  247. catch (Exception)
  248. {
  249. }
  250. }
  251. result.Add(dic);
  252. return result;
  253. }
  254. /// <summary>
  255. /// 单一类型集合,如果Object类型不一致,请手动使用GetSheetData(object o)
  256. /// </summary>
  257. /// <param name="objects"></param>
  258. /// <returns></returns>
  259. public static List<Dictionary<string, object>> GetSheetData(List<object> objects)
  260. {
  261. List<Dictionary<string, object>> dic = new List<Dictionary<string, object>>();
  262. if (!objects.Any())
  263. return dic;
  264. Type type = objects[0].GetType();
  265. var properties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  266. Dictionary<string, PropertyInfo> pros = new Dictionary<string, PropertyInfo>();
  267. foreach (var property in properties)
  268. {
  269. try
  270. {
  271. if ((property.PropertyType.IsClass&&property.PropertyType!=typeof(string))|| property.PropertyType.IsAbstract)
  272. continue;
  273. TSheetFileAttribute[] attrs =
  274. property.GetCustomAttributes(typeof(TSheetFileAttribute), true) as TSheetFileAttribute[];
  275. if (attrs.Any())
  276. {
  277. pros[attrs[0].FileName] = property;
  278. }
  279. }
  280. catch (Exception)
  281. {
  282. }
  283. }
  284. for (int i = 0; i < objects.Count; i++)
  285. {
  286. object tempObject = objects[i];
  287. ITSheetData iTSheet = tempObject as ITSheetData;
  288. if (iTSheet != null)
  289. {
  290. dic.AddRange(iTSheet.CreateSheetData());
  291. continue;
  292. }
  293. Dictionary<string, object> tempDicObject = new Dictionary<string, object>();
  294. foreach (var propertyInfo in pros)
  295. {
  296. tempDicObject.Add(propertyInfo.Key, propertyInfo.Value.GetValue(tempObject));
  297. }
  298. dic.Add(tempDicObject);
  299. }
  300. return dic;
  301. }
  302. public static int GetRowCount(object o)
  303. {
  304. //其他情况都是1,继承自ITSheetData时自动获取
  305. //ITSheetData tData = o as ITSheetData;
  306. return GetSheetData(o).Count;
  307. }
  308. #endregion
  309. }
  310. public class TCell
  311. {
  312. public TCell()
  313. {
  314. this.RowSpan = 1;
  315. this.ColumnSpan = 1;
  316. }
  317. /// <summary>
  318. /// 列索引
  319. /// </summary>
  320. public int ColumnIndex { get; set; }
  321. /// <summary>
  322. ///行索引
  323. /// </summary>
  324. public int RowIndex { get; set; }
  325. /// <summary>
  326. /// 行跨度
  327. /// </summary>
  328. public int RowSpan { get; set; }
  329. /// <summary>
  330. /// 列跨度
  331. /// </summary>
  332. public int ColumnSpan { get; set; }
  333. /// <summary>
  334. /// 单元格值
  335. /// </summary>
  336. public object Value { get; set; }
  337. /// <summary>
  338. /// 标签扩展
  339. /// </summary>
  340. public object Tag { get; set; }
  341. }
  342. /// <summary>
  343. /// 绘图逻辑
  344. /// </summary>
  345. public interface ITSheetContext
  346. {
  347. void Draw(TSheet sheet);
  348. }
  349. /// <summary>
  350. /// 获取表格数据
  351. /// </summary>
  352. public interface ITSheetData
  353. {
  354. /// <summary>
  355. /// 创建表格数据
  356. /// </summary>
  357. /// <returns></returns>
  358. List<Dictionary<string, object>> CreateSheetData();
  359. }
  360. }