using Autodesk.Revit.DB; using Autodesk.Revit.UI; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace CivilModelCreator { internal class ProcessEventHandler : IExternalEventHandler { public ProgressMonitorView CurrentUI { get; set; } public ProgressMonitorControl CurrentControl { get; set; } private bool Cancel = false; public List CdmData { get; set; } public string Path { get; set; } private delegate void ProgressBarDelegate(); public void Execute(UIApplication app) { if (app == null) { CloseWindow(); return; } if (app.ActiveUIDocument == null || app.ActiveUIDocument.Document == null || CdmData == null || CurrentUI == null || CurrentControl == null || Path == null) return; CurrentUI.btnCancel.Click += CurrentUI_Closed; for (CurrentControl.CurrentValue = 0; CurrentControl.CurrentValue < CurrentControl.MaxValue; ++CurrentControl.CurrentValue) { int index = Convert.ToInt16(CurrentControl.CurrentValue); if (Cancel) break; System.Threading.Thread.Sleep(10); #region 做操作 try { CDMtoExcel(CdmData[index]); } catch (Exception e) { MessageBox.Show(e.Message); //CloseWindow(); return; } #endregion 做操作 CurrentControl.CurrentContext = string.Format("进度已完成 {0} of {1} ", CurrentControl.CurrentValue, CurrentControl.MaxValue); CurrentUI.Dispatcher.Invoke(new ProgressBarDelegate(CurrentControl.NotifyUI), System.Windows.Threading.DispatcherPriority.Background); } CloseWindow(); } private void CDMtoExcel(CDMData cdm) { XSSFWorkbook workbook = new XSSFWorkbook(); ISheet firstSheet = workbook.CreateSheet("CDM数据表"); #region 表格 firstSheet.CreateRow(0); firstSheet.GetRow(0).CreateCell(0); firstSheet.GetRow(0).CreateCell(1); firstSheet.GetRow(0).CreateCell(2); firstSheet.GetRow(0).GetCell(0).SetCellValue("HcdmClass"); firstSheet.GetRow(0).GetCell(1).SetCellValue(cdm.Category + "CDM专用标准"); firstSheet.GetRow(0).GetCell(2).SetCellValue(cdm.HcdmClass); firstSheet.CreateRow(1); firstSheet.GetRow(1).CreateCell(0); firstSheet.GetRow(1).CreateCell(1); firstSheet.GetRow(1).GetCell(0).SetCellValue("构件名称"); firstSheet.GetRow(1).GetCell(1).SetCellValue(cdm.Category); firstSheet.CreateRow(2); firstSheet.GetRow(2).CreateCell(0); firstSheet.GetRow(2).CreateCell(1); firstSheet.GetRow(2).CreateCell(2); firstSheet.GetRow(2).GetCell(0).SetCellValue("HcdmClass"); firstSheet.GetRow(2).GetCell(1).SetCellValue("XXXXX(自动生成)"); firstSheet.GetRow(2).GetCell(2).SetCellValue(cdm.HcdmNumber); firstSheet.CreateRow(4); firstSheet.GetRow(4).CreateCell(0); firstSheet.GetRow(4).CreateCell(1); firstSheet.GetRow(4).CreateCell(2); firstSheet.GetRow(4).CreateCell(3); firstSheet.GetRow(4).CreateCell(4); firstSheet.GetRow(4).GetCell(0).SetCellValue("字段编码"); firstSheet.GetRow(4).GetCell(1).SetCellValue("字段名称"); firstSheet.GetRow(4).GetCell(2).SetCellValue("单位"); firstSheet.GetRow(4).GetCell(3).SetCellValue("备注"); firstSheet.GetRow(4).GetCell(4).SetCellValue("字段值"); firstSheet.CreateRow(5); firstSheet.GetRow(5).CreateCell(0); firstSheet.GetRow(5).CreateCell(1); firstSheet.GetRow(5).GetCell(0).SetCellValue("101"); firstSheet.GetRow(5).GetCell(1).SetCellValue("编号(自定义)"); firstSheet.CreateRow(6); firstSheet.GetRow(6).CreateCell(0); firstSheet.GetRow(6).CreateCell(1); firstSheet.GetRow(6).CreateCell(2); firstSheet.GetRow(6).CreateCell(4); firstSheet.GetRow(6).GetCell(0).SetCellValue("102"); firstSheet.GetRow(6).GetCell(1).SetCellValue(cdm.Category + "底标高"); firstSheet.GetRow(6).GetCell(2).SetCellValue("m"); firstSheet.GetRow(6).GetCell(4).SetCellValue(Math.Round(cdm.BaseLevel, 2, MidpointRounding.AwayFromZero)); firstSheet.CreateRow(7); firstSheet.GetRow(7).CreateCell(0); firstSheet.GetRow(7).CreateCell(1); firstSheet.GetRow(7).CreateCell(2); firstSheet.GetRow(7).CreateCell(4); firstSheet.GetRow(7).GetCell(0).SetCellValue("103"); firstSheet.GetRow(7).GetCell(1).SetCellValue(cdm.Category + "中心点坐标X"); firstSheet.GetRow(7).GetCell(2).SetCellValue("m"); firstSheet.GetRow(7).GetCell(4).SetCellValue(Math.Round(cdm.Center_X, 2, MidpointRounding.AwayFromZero)); firstSheet.CreateRow(8); firstSheet.GetRow(8).CreateCell(0); firstSheet.GetRow(8).CreateCell(1); firstSheet.GetRow(8).CreateCell(2); firstSheet.GetRow(8).CreateCell(4); firstSheet.GetRow(8).GetCell(0).SetCellValue("104"); firstSheet.GetRow(8).GetCell(1).SetCellValue(cdm.Category + "中心点坐标Y"); firstSheet.GetRow(8).GetCell(2).SetCellValue("m"); firstSheet.GetRow(8).GetCell(4).SetCellValue(Math.Round(cdm.Center_Y, MidpointRounding.AwayFromZero)); firstSheet.CreateRow(9); firstSheet.GetRow(9).CreateCell(0); firstSheet.GetRow(9).CreateCell(1); firstSheet.GetRow(9).CreateCell(2); firstSheet.GetRow(9).CreateCell(4); firstSheet.GetRow(9).GetCell(0).SetCellValue("105"); firstSheet.GetRow(9).GetCell(1).SetCellValue(cdm.Category + "边长X"); firstSheet.GetRow(9).GetCell(2).SetCellValue("m"); firstSheet.GetRow(9).GetCell(4).SetCellValue(Math.Round(cdm.Length, 2, MidpointRounding.AwayFromZero)); firstSheet.CreateRow(10); firstSheet.GetRow(10).CreateCell(0); firstSheet.GetRow(10).CreateCell(1); firstSheet.GetRow(10).CreateCell(2); firstSheet.GetRow(10).CreateCell(4); firstSheet.GetRow(10).GetCell(0).SetCellValue("106"); firstSheet.GetRow(10).GetCell(1).SetCellValue(cdm.Category + "边长Y"); firstSheet.GetRow(10).GetCell(2).SetCellValue("m"); firstSheet.GetRow(10).GetCell(4).SetCellValue(Math.Round(cdm.Width, 2, MidpointRounding.AwayFromZero)); firstSheet.CreateRow(11); firstSheet.GetRow(11).CreateCell(0); firstSheet.GetRow(11).CreateCell(1); firstSheet.GetRow(11).CreateCell(2); firstSheet.GetRow(11).CreateCell(4); firstSheet.GetRow(11).GetCell(0).SetCellValue("107"); firstSheet.GetRow(11).GetCell(1).SetCellValue(cdm.Category + "顶标高"); firstSheet.GetRow(11).GetCell(2).SetCellValue("m"); firstSheet.GetRow(11).GetCell(4).SetCellValue(Math.Round(cdm.TopLevel, 2, MidpointRounding.AwayFromZero)); firstSheet.CreateRow(12); firstSheet.GetRow(12).CreateCell(0); firstSheet.GetRow(12).CreateCell(1); firstSheet.GetRow(12).CreateCell(2); firstSheet.GetRow(12).CreateCell(4); firstSheet.GetRow(12).GetCell(0).SetCellValue("108"); firstSheet.GetRow(12).GetCell(1).SetCellValue(cdm.Category + "身材料"); firstSheet.GetRow(12).GetCell(2).SetCellValue("混凝土、砖、其他"); firstSheet.GetRow(12).GetCell(4).SetCellValue(cdm.Material); firstSheet.CreateRow(13); firstSheet.GetRow(13).CreateCell(0); firstSheet.GetRow(13).CreateCell(1); firstSheet.GetRow(13).CreateCell(2); firstSheet.GetRow(13).CreateCell(4); firstSheet.GetRow(13).GetCell(0).SetCellValue("109"); firstSheet.GetRow(13).GetCell(1).SetCellValue(cdm.Category + "材料强度(标准值)"); firstSheet.GetRow(13).GetCell(2).SetCellValue("MPa"); firstSheet.GetRow(13).GetCell(4).SetCellValue(cdm.Strength); firstSheet.CreateRow(14); firstSheet.GetRow(14).CreateCell(0); firstSheet.GetRow(14).CreateCell(1); firstSheet.GetRow(14).CreateCell(2); firstSheet.GetRow(14).CreateCell(4); firstSheet.GetRow(14).GetCell(0).SetCellValue("110"); firstSheet.GetRow(14).GetCell(1).SetCellValue("设计负责人"); firstSheet.GetRow(14).GetCell(2).SetCellValue("姓名"); firstSheet.GetRow(14).GetCell(4).SetCellValue(cdm.Designer); firstSheet.CreateRow(15); firstSheet.GetRow(15).CreateCell(0); firstSheet.GetRow(15).CreateCell(1); //firstSheet.GetRow(15).CreateCell(2); firstSheet.GetRow(15).CreateCell(4); firstSheet.GetRow(15).GetCell(0).SetCellValue("111"); firstSheet.GetRow(15).GetCell(1).SetCellValue("身份证"); //firstSheet.GetRow(15).GetCell(2).SetCellValue(""); firstSheet.GetRow(15).GetCell(4).SetCellValue(cdm.Iden); firstSheet.AutoSizeColumn(0); firstSheet.AutoSizeColumn(1); firstSheet.AutoSizeColumn(2); firstSheet.AutoSizeColumn(3); firstSheet.AutoSizeColumn(4); #endregion 表格 using (FileStream excelStream = File.Create(Path + "\\" + cdm.HcdmClass + "-" + cdm.HcdmNumber + ".xlsx")) { workbook.Write(excelStream); } } private void CloseWindow() { CurrentUI.Closed -= CurrentUI_Closed; CurrentUI.Close(); } private void CurrentUI_Closed(object sender, EventArgs e) { Cancel = true; } public string GetName() { return "处理进度"; } } }