123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- /* ==============================================================================
- * 功能描述:导出所有的岗位
- * 创 建 者:Garrett
- * 创建日期:2018/8/10 16:36:26
- * ==============================================================================*/
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Autodesk.Revit.DB;
- using Autodesk.Revit.DB.Mechanical;
- using FWindSoft.Tools;
- using Microsoft.Win32;
- using Newtonsoft.Json.Linq;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using SAGA.DotNetUtils;
- using SAGA.DotNetUtils.Logger;
- using SAGA.DotNetUtils.Others;
- using SAGA.MBI.Calc;
- using SAGA.MBI.Common;
- using SAGA.MBI.DataArrange;
- using SAGA.MBI.Model;
- using SAGA.MBI.RequestData;
- using SAGA.MBI.Tools;
- using SAGA.MBI.WinView.Upload;
- using SAGA.RevitUtils.Extends;
- using SAGA.RevitUtils.MEP;
- using CellType = Autodesk.Revit.DB.CellType;
- namespace SAGA.MBI.ToolsData
- {
- /// <summary>
- /// UpdateRelationEquipinFloor
- /// </summary>
- public class ExportMEPSystem
- {
- /// <summary>
- /// 检查并处理所有楼层
- /// </summary>
- public static void OperateAll()
- {
- var floors = DalProjectTree.GetAllFloors(false);
- List<ExportItemGroup> contexts = new List<ExportItemGroup>();
- foreach (var floor in floors)
- {
- contexts.Add(Operate(floor));
- }
- ExportToExcel(contexts);
- }
- /// <summary>
- /// 只处理当前楼层
- /// </summary>
- public static void OperateCurFloor()
- {
- MFloor floor = ExternalDataWrapper.Current.Doc.GetCurMFloor();
- if (floor != null)
- {
- var context = Operate(floor);
- ExportToExcel(new List<ExportItemGroup>() { context });
- }
- }
- /// <summary>
- /// 检查并处理
- /// </summary>
- /// <param name="floor"></param>
- /// <returns></returns>
- private static ExportItemGroup Operate(MFloor floor)
- {
- string fullPah = floor.FullPath;
- Document doc = ExternalDataWrapper.Current.UiApp.Application.OpenDocumentFile(fullPah);
- ExportItemGroup group =new ExportItemGroup(floor.ToString());
- var systems = doc.GetElements<MEPSystem>();
- foreach (var mepSystem in systems)
- {
- string name = mepSystem.Name;
- string systemType = doc.GetElement(mepSystem.GetTypeId()).Name;
- string type = mepSystem is MechanicalSystem ? "风管" : "水管";
- group.Items.Add(new ExportItem(){SystemName = name,SystemType = systemType,Type = type});
- }
- return group;
- }
- /// <summary>
- /// 导出数据
- /// </summary>
- /// <param name="xyzsList"></param>
- static void ExportToExcel(List<ExportItemGroup> contexts)
- {
- SaveFileDialog sflg = new SaveFileDialog();
- sflg.Filter = "Excel(*.xlsx)|*.xlsx";
- sflg.FileName = $"{MBIControl.ProjectCur}系统汇总表";
- if (sflg.ShowDialog() != true) return;
- ExportToExcel(contexts, sflg.FileName, sflg.FilterIndex);
- }
- /// <summary>
- /// 导出数据到指定目录
- /// </summary>
- /// <param name="xyzsList"></param>
- /// <param name="fileName"></param>
- private static void ExportToExcel(List<ExportItemGroup> contexts, string fileName, int filterIndex = 0)
- {
- try
- {
- IWorkbook book = new XSSFWorkbook();
- #region 添加数据
- foreach (var context in contexts)
- {
- int index = 0;
- var floorName = context.FloorName;
- ISheet sheet = book.CreateSheet(floorName);
- #region 添加表头
- IRow row = sheet.CreateRow(0);
- NPOI.SS.UserModel.ICell cell0 = row.CreateCell(0);
- cell0.SetCellType(NPOI.SS.UserModel.CellType.String);
- cell0.SetCellValue("楼层名称");
- ICell cell1 = row.CreateCell(1);
- cell1.SetCellType(NPOI.SS.UserModel.CellType.String);
- cell1.SetCellValue("类型");
- ICell cell2 = row.CreateCell(2);
- cell2.SetCellType(NPOI.SS.UserModel.CellType.String);
- cell2.SetCellValue("系统名称");
- ICell cell3 = row.CreateCell(3);
- cell3.SetCellType(NPOI.SS.UserModel.CellType.String);
- cell3.SetCellValue("系统类型名称");
- #endregion
-
- foreach (var item in context.Items)
- {
- string type = item.Type;
- string name = item.SystemName;
- string sytemName = item.SystemType;
- index++;
- row = sheet.CreateRow(index);
- ICell cell = row.CreateCell(0, NPOI.SS.UserModel.CellType.String);
- cell.SetCellValue(floorName);
- cell = row.CreateCell(1, NPOI.SS.UserModel.CellType.String);
- cell.SetCellValue(type);
- cell = row.CreateCell(2, NPOI.SS.UserModel.CellType.String);
- cell.SetCellValue(name);
- cell = row.CreateCell(3, NPOI.SS.UserModel.CellType.String);
- cell.SetCellValue(sytemName);
- }
- }
- #endregion
- #region 写入
- MemoryStream ms = new MemoryStream();
- book.Write(ms);
- book = null;
- using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
- {
- byte[] data = ms.ToArray();
- fs.Write(data, 0, data.Length);
- fs.Flush();
- }
- ms.Close();
- ms.Dispose();
- #endregion
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- }
- /// <summary>
- /// 导出数据到指定目录
- /// </summary>
- /// <param name="xyzsList"></param>
- /// <param name="fileName"></param>
- private static void ExportToExcel2(List<CalcContext> contexts, string fileName, int filterIndex = 0)
- {
- try
- {
- IWorkbook book = new XSSFWorkbook();
- #region 添加数据
- ISheet sheet = book.CreateSheet("Summary");
- #region 添加表头
- IRow row = sheet.CreateRow(0);
- NPOI.SS.UserModel.ICell cell0 = row.CreateCell(0);
- cell0.SetCellType(NPOI.SS.UserModel.CellType.String);
- cell0.SetCellValue("楼层名称");
- ICell cell1 = row.CreateCell(1);
- cell1.SetCellType(NPOI.SS.UserModel.CellType.String);
- cell1.SetCellValue("类型");
- ICell cell2 = row.CreateCell(2);
- cell2.SetCellType(NPOI.SS.UserModel.CellType.String);
- cell2.SetCellValue("总数");
- List<string> list = new List<string>() { "FASE-光电感烟探测器", "FASE-智能感温探测器", "FSCP-消火栓起泵按钮" };
- var list2 = list.Select(tt => tt.Substring(0, 4)).ToList();
- int index = 0;
- foreach (var context in contexts)
- {
- var floorName = context.MFloor.ToString();
- #endregion
- Action<int> action = (count) =>
- {
- index++;
- row = sheet.CreateRow(index);
- ICell cell = row.CreateCell(0, NPOI.SS.UserModel.CellType.String);
- cell.SetCellValue(floorName);
- cell = row.CreateCell(1, NPOI.SS.UserModel.CellType.String);
- cell.SetCellValue(string.Join(",", list));
- cell = row.CreateCell(2, NPOI.SS.UserModel.CellType.String);
- cell.SetCellValue(count);
- };
- action(context.MEquipments.Where(t => list2.Contains(t.EquipClassCode)).Count());
- }
- #endregion
- #region 写入
- MemoryStream ms = new MemoryStream();
- book.Write(ms);
- book = null;
- using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
- {
- byte[] data = ms.ToArray();
- fs.Write(data, 0, data.Length);
- fs.Flush();
- }
- ms.Close();
- ms.Dispose();
- #endregion
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- }
- class ExportItemGroup
- {
- public ExportItemGroup(string floorName)
- {
- FloorName = floorName;
- Items = new List<ExportItem>();
- }
- public List<ExportItem> Items { get; set; }
- public string FloorName { get; set; }
- }
- class ExportItem
- {
- public string SystemName { get; set; }
- public string SystemType { get; set; }
- public string Type { get; set; }
- }
- }
- }
|