ExportAllDuty.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* ==============================================================================
  2. * 功能描述:导出所有的岗位
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/8/10 16:36:26
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Autodesk.Revit.DB;
  13. using Autodesk.Revit.DB.Mechanical;
  14. using Microsoft.Win32;
  15. using Newtonsoft.Json.Linq;
  16. using NPOI.SS.UserModel;
  17. using NPOI.XSSF.UserModel;
  18. using SAGA.DotNetUtils;
  19. using SAGA.DotNetUtils.Logger;
  20. using SAGA.DotNetUtils.Others;
  21. using SAGA.MBI.Calc;
  22. using SAGA.MBI.Common;
  23. using SAGA.MBI.DataArrange;
  24. using SAGA.MBI.Model;
  25. using SAGA.MBI.RequestData;
  26. using SAGA.MBI.Tools;
  27. using SAGA.MBI.WinView.Upload;
  28. using SAGA.RevitUtils.Extends;
  29. using CellType = Autodesk.Revit.DB.CellType;
  30. namespace SAGA.MBI.ToolsData
  31. {
  32. /// <summary>
  33. /// UpdateRelationEquipinFloor
  34. /// </summary>
  35. public class ExportAllDuty
  36. {
  37. /// <summary>
  38. /// 检查并处理所有楼层
  39. /// </summary>
  40. public static void OperateAll()
  41. {
  42. var floors = DalUploadFloor.GetHasFileFloors();
  43. List<CalcContext> contexts = new List<CalcContext>();
  44. foreach (UploadFloor floor in floors)
  45. {
  46. contexts.Add(Operate(floor.MFloor));
  47. }
  48. ExportToExcel(contexts);
  49. }
  50. /// <summary>
  51. /// 只处理当前楼层
  52. /// </summary>
  53. public static void OperateCurFloor()
  54. {
  55. MFloor floor = ExternalDataWrapper.Current.Doc.GetCurMFloor();
  56. if (floor != null)
  57. {
  58. var context = Operate(floor);
  59. ExportToExcel(new List<CalcContext>() { context });
  60. }
  61. }
  62. /// <summary>
  63. /// 检查并处理
  64. /// </summary>
  65. /// <param name="floor"></param>
  66. /// <returns></returns>
  67. private static CalcContext Operate(MFloor floor)
  68. {
  69. var context = DalCommon.DownLoadFloorDataByBIMFloorInfo(floor);
  70. return context;
  71. }
  72. /// <summary>
  73. /// 导出数据
  74. /// </summary>
  75. /// <param name="xyzsList"></param>
  76. public static void ExportToExcel(List<CalcContext> contexts)
  77. {
  78. SaveFileDialog sflg = new SaveFileDialog();
  79. sflg.Filter = "Excel(*.xlsx)|*.xlsx";
  80. sflg.FileName = $"{MBIControl.ProjectCur}岗位汇总表";
  81. if (sflg.ShowDialog() != true) return;
  82. ExportToExcel(contexts, sflg.FileName, sflg.FilterIndex);
  83. }
  84. /// <summary>
  85. /// 导出数据到指定目录
  86. /// </summary>
  87. /// <param name="xyzsList"></param>
  88. /// <param name="fileName"></param>
  89. private static void ExportToExcel(List<CalcContext> contexts, string fileName, int filterIndex = 0)
  90. {
  91. try
  92. {
  93. IWorkbook book = new XSSFWorkbook();
  94. #region 添加数据
  95. foreach (var context in contexts)
  96. {
  97. int index = 0;
  98. var floorName = context.MFloor.ToString();
  99. var floorPath = context.MFloor.FullPath;
  100. ISheet sheet = book.CreateSheet(floorName);
  101. #region 添加表头
  102. IRow row = sheet.CreateRow(0);
  103. NPOI.SS.UserModel.ICell cell0 = row.CreateCell(0);
  104. cell0.SetCellType(NPOI.SS.UserModel.CellType.String);
  105. cell0.SetCellValue("楼层名称");
  106. ICell cell1 = row.CreateCell(1);
  107. cell1.SetCellType(NPOI.SS.UserModel.CellType.String);
  108. cell1.SetCellValue("类型");
  109. ICell cell2 = row.CreateCell(2);
  110. cell2.SetCellType(NPOI.SS.UserModel.CellType.String);
  111. cell2.SetCellValue("族名称");
  112. ICell cell3 = row.CreateCell(3);
  113. cell3.SetCellType(NPOI.SS.UserModel.CellType.String);
  114. cell3.SetCellValue("物理世界Id");
  115. ICell cell4 = row.CreateCell(4);
  116. cell4.SetCellType(NPOI.SS.UserModel.CellType.String);
  117. cell4.SetCellValue("模型中的Id");
  118. #endregion
  119. Action<MRevitEquipBase, string> action = (equip, type) =>
  120. {
  121. string family = equip.EquipClassCode+"-"+equip.Family?.Name;
  122. var id = equip.Id;
  123. var bimid = equip.BimID.GetBIMID();
  124. index++;
  125. row = sheet.CreateRow(index);
  126. ICell cell = row.CreateCell(0, NPOI.SS.UserModel.CellType.String);
  127. cell.SetCellValue(floorName);
  128. cell = row.CreateCell(1, NPOI.SS.UserModel.CellType.String);
  129. cell.SetCellValue(type);
  130. cell = row.CreateCell(2, NPOI.SS.UserModel.CellType.String);
  131. cell.SetCellValue(family);
  132. cell = row.CreateCell(3, NPOI.SS.UserModel.CellType.String);
  133. cell.SetCellValue(id);
  134. cell = row.CreateCell(4, NPOI.SS.UserModel.CellType.String);
  135. cell.SetCellValue(bimid);
  136. };
  137. foreach (var equip in context.MEquipments)
  138. {
  139. action(equip, "设备");
  140. }
  141. foreach (var part in context.MEquipmentParts)
  142. {
  143. action(part, "部件");
  144. }
  145. foreach (var beacon in context.MBeacons)
  146. {
  147. action(beacon, "信标");
  148. }
  149. foreach (var space in context.MSpaces)
  150. {
  151. action(space, "空间");
  152. }
  153. }
  154. #endregion
  155. #region 写入
  156. MemoryStream ms = new MemoryStream();
  157. book.Write(ms);
  158. book = null;
  159. using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
  160. {
  161. byte[] data = ms.ToArray();
  162. fs.Write(data, 0, data.Length);
  163. fs.Flush();
  164. }
  165. ms.Close();
  166. ms.Dispose();
  167. #endregion
  168. }
  169. catch (Exception e)
  170. {
  171. MessageShowBase.Show(e);
  172. }
  173. }
  174. /// <summary>
  175. /// 导出数据到指定目录
  176. /// </summary>
  177. /// <param name="xyzsList"></param>
  178. /// <param name="fileName"></param>
  179. private static void ExportToExcel2(List<CalcContext> contexts, string fileName, int filterIndex = 0)
  180. {
  181. try
  182. {
  183. IWorkbook book = new XSSFWorkbook();
  184. #region 添加数据
  185. ISheet sheet = book.CreateSheet("Summary");
  186. #region 添加表头
  187. IRow row = sheet.CreateRow(0);
  188. NPOI.SS.UserModel.ICell cell0 = row.CreateCell(0);
  189. cell0.SetCellType(NPOI.SS.UserModel.CellType.String);
  190. cell0.SetCellValue("楼层名称");
  191. ICell cell1 = row.CreateCell(1);
  192. cell1.SetCellType(NPOI.SS.UserModel.CellType.String);
  193. cell1.SetCellValue("类型");
  194. ICell cell2 = row.CreateCell(2);
  195. cell2.SetCellType(NPOI.SS.UserModel.CellType.String);
  196. cell2.SetCellValue("总数");
  197. List<string> list = new List<string>() { "FASE-光电感烟探测器", "FASE-智能感温探测器", "FSCP-消火栓起泵按钮" };
  198. var list2 = list.Select(tt => tt.Substring(0, 4)).ToList();
  199. int index = 0;
  200. foreach (var context in contexts)
  201. {
  202. var floorName = context.MFloor.ToString();
  203. #endregion
  204. Action<int> action = (count) =>
  205. {
  206. index++;
  207. row = sheet.CreateRow(index);
  208. ICell cell = row.CreateCell(0, NPOI.SS.UserModel.CellType.String);
  209. cell.SetCellValue(floorName);
  210. cell = row.CreateCell(1, NPOI.SS.UserModel.CellType.String);
  211. cell.SetCellValue(string.Join(",", list));
  212. cell = row.CreateCell(2, NPOI.SS.UserModel.CellType.String);
  213. cell.SetCellValue(count);
  214. };
  215. action(context.MEquipments.Where(t => list2.Contains(t.EquipClassCode)).Count());
  216. }
  217. #endregion
  218. #region 写入
  219. MemoryStream ms = new MemoryStream();
  220. book.Write(ms);
  221. book = null;
  222. using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
  223. {
  224. byte[] data = ms.ToArray();
  225. fs.Write(data, 0, data.Length);
  226. fs.Flush();
  227. }
  228. ms.Close();
  229. ms.Dispose();
  230. #endregion
  231. }
  232. catch (Exception e)
  233. {
  234. MessageShowBase.Show(e);
  235. }
  236. }
  237. }
  238. }