using FluentEmail.Core; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using NPOI.HPSF; using NPOI.SS.Formula.Functions; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using Ropin.Inspection.Api.Common; using Ropin.Inspection.Api.Controllers; using Ropin.Inspection.Common.Helper; using Ropin.Inspection.Model; using Ropin.Inspection.Model.Common; using Ropin.Inspection.Model.ViewModel.DEV; using Ropin.Inspection.Service; using Ropin.Inspection.Service.DEV.Interface; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Drawing.Printing; using System.IO; using System.IO.Compression; using System.Linq; using System.Net; using System.Net.Http; using System.Runtime.ConstrainedExecution; using System.Threading.Tasks; namespace Ropin.Inspection.Api { public class TdevDevOpeAccountController : BaseController { public ILogger _logger { get; } private readonly ITdevDevOpeAccountService _TdevDevOpeAccountService; private readonly IDevDevOpeAccountConfigService _devDevOpeAccountConfigService; /// /// 构造函数 /// /// /// public TdevDevOpeAccountController(ITdevDevOpeAccountService TdevDevOpeAccountService, IDevDevOpeAccountConfigService devDevOpeAccountConfigService, ILogger logger) { _TdevDevOpeAccountService = TdevDevOpeAccountService; _logger = logger; _devDevOpeAccountConfigService = devDevOpeAccountConfigService; } /// /// 通过id获取业主设备运行台账信息 /// /// /// [HttpGet("GetDevOpeAccountAsync/{id}")] public async Task GetDevOpeAccountAsync(string id) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TdevDevOpeAccountService.GetConditionAsync(new TdevDevOpeAccountSearchModel { C_ID = id }); return new ApiResult(content.FirstOrDefault()); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 获取所有业主设备运行台账 /// /// [HttpGet("GetDevOpeAccountsAsync")] public async Task GetDevOpeAccountsAsync() { try { var contentList = await _TdevDevOpeAccountService.GetAllAsync(); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过业主设备运行台账名称条件查询 /// /// /// [HttpPost("GetDevOpeAccountsByAsync")] public async Task GetDevOpeAccountsByAsync(TdevDevOpeAccountSearchModel searchModel) { if (searchModel == null) { return new ApiResult(ReturnCode.ArgsError); } searchModel.IsPagination = false; try { var contentList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel); return new ApiResult>(new PagesModel(contentList, searchModel)); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 查询运行台账 /// /// /// [HttpPost("GetDevOpeAccountContentByAsync")] [AllowAnonymous] public async Task GetDevOpeAccountContentByAsync(TdevDevOpeAccountSearchModel searchModel) { if (searchModel == null) { return new ApiResult(ReturnCode.ArgsError); } try { var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel); if (devOpeAccountList==null|| devOpeAccountList.Count()==0) { return new ApiResult(new { tabName = new string[0], pages = new PagesModel(null, searchModel) }); } List tabs = new List(); PagesModel pages = null; var confCode = searchModel.C_DevOpeAccountConfigCode; if (!string.IsNullOrEmpty(confCode)) { tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date", BReadDevSpot=false }); var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode); if (content != null && !string.IsNullOrEmpty(content.C_Config)) { var RunSpotConfig = JsonConvert.DeserializeObject(content.C_Config); foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList()) { if (item!=null) { if (item.BReadDevSpot) { //string labVal = "";string labVal1 =""; //if (item.Label.Contains('(')|| item.Label.Contains('(')) //{ // labVal = item.Label.Replace("(","(").Replace("(","最大值("); // labVal1 = item.Label.Replace("(", "(").Replace("(", "最小值("); //} //else //{ // labVal = item.Label + "最大值"; // labVal1 = item.Label + "最小值"; //} tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot }); tabs.Add(new RunSpotConfigName { Label = item.Label+ "@最大值", Name = item.Name+ "_Max", BReadDevSpot = item.BReadDevSpot }); tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot }); } else { tabs.Add(item); } } } } } var confData = devOpeAccountList.Select(t => t.C_Content).ToList(); List list = new List(); for (int i = 0; i < confData.Count; i++) { Dictionary dict = JsonConvert.DeserializeObject>(confData[i]); if (dict.ContainsKey("RunWhetherNormal")) { var RunWhetherNormal = dict["RunWhetherNormal"].ToString(); dict["RunWhetherNormal"] = RunWhetherNormal.ToLower() == "true" ? "异常" : "正常"; } var trueList = tabs?.Where(t => t.BReadDevSpot == true); foreach (var item in trueList) { if (item!=null) { if (!dict.ContainsKey(item.Name)) { dict.Add(item.Name, "0"); } } } foreach (var item in dict) { if (item.Key?.ToString()== "Date") { dict[item.Key] = Convert.ToDateTime(item.Value).ToString("yyyy-MM-dd"); } else { if (item.Value != null) { if (DateTime.TryParse(item.Value.ToString(), out DateTime dt) && item.Value.ToString().Length >= 10) { dict[item.Key] = dt.ToString("yyyy-MM-dd HH:mm:ss"); } } } } list.Add(dict); } pages = new PagesModel(list, searchModel); //var tabArray = tabs.Select(t => t.Label).ToArray(); List tabList = new List(); for (int i = 0; i < tabs.Count; i++) { var vals = tabs[i].Label.ToString(); var name = tabs[i].Name.ToString(); if (vals.Contains("@")) { string[] tmp = vals.Split('@');//按,分割字符串 var TableHeadeModel = tabList.Find(t => t.label == tmp[0].ToString()); if (TableHeadeModel!=null) { if (TableHeadeModel.children!=null&& TableHeadeModel.children.Count>0) { if (tmp.Count() == 2) { TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[1].ToString(), Level = 2, children = null }); } if (tmp.Count() == 3) { var childModel = TableHeadeModel.children.Find(x => x.label == tmp[1].ToString()); if (childModel!=null) { if (childModel.children==null) { childModel.children = new List(); } childModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children = null }); } else { List chile = null; chile = new List(); chile.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children = null }); TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[1].ToString(), Level = 2, children = chile }); } } } else { TableHeadeModel.children = new List(); if (tmp.Count() == 2) { TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[1].ToString(), Level = 2, children = null }); } if (tmp.Count() == 3) { List chile = null; chile = new List(); chile.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children = null }); TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[1].ToString(), Level = 2, children = chile }); } } } else { var cc = new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[0].ToString(),Level=1, children = new List()}; if (tmp.Count() == 2) { cc.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[1].ToString(), Level = 2, children = null }); } if (tmp.Count()==3) { List chile = null; chile = new List(); chile.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children =null}); cc.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[1].ToString(), Level = 2, children = chile }); } tabList.Add(cc); } } else { tabList.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label=vals, Level = 1, children = null}); } } var data = new { DevOpeAccountConfigCode= confCode, tabName = tabs.Select(t => t.Label).ToArray(), tabArray= tabList, pages }; return new ApiResult(data); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 查询运行台账-导出 /// /// /// [HttpPost("GetDevOpeAccountContentExportByAsync")] [AllowAnonymous] public async Task GetDevOpeAccountContentExportByAsync(TdevDevOpeAccountSearchModel searchModel) { if (searchModel == null) { return new ApiResult(ReturnCode.ArgsError); } try { var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel); if (devOpeAccountList == null || devOpeAccountList.Count() == 0) { return new ApiResult(new { tabName = new string[0], pages = new PagesModel(null, searchModel) }); } List tabs = new List(); PagesModel pages = null; var confCode = searchModel.C_DevOpeAccountConfigCode; if (!string.IsNullOrEmpty(confCode)) { tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date" ,BReadDevSpot=false }); var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode); if (content != null && !string.IsNullOrEmpty(content.C_Config)) { var RunSpotConfig = JsonConvert.DeserializeObject(content.C_Config); foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList()) { if (item != null) { if (item.BReadDevSpot) { //string labVal = ""; string labVal1 = ""; //if (item.Label.Contains('(') || item.Label.Contains('(')) //{ // labVal = item.Label.Replace("(", "(").Replace("(", "最大值("); // labVal1 = item.Label.Replace("(", "(").Replace("(", "最小值("); //} //else //{ // labVal = item.Label + "最大值"; // labVal1 = item.Label + "最小值"; //} tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot }); tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot }); tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot }); } else { tabs.Add(item); } } } } } var confData = devOpeAccountList.Select(t => t.C_Content).ToList(); List list = new List(); for (int i = 0; i < confData.Count; i++) { Dictionary dict = JsonConvert.DeserializeObject>(confData[i]); string[] cels = new string[tabs.Count]; int forCount = dict.Count + 1; if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; } for (int j = 0; j < forCount; j++) { if (j < tabs.Count) { string itemVal = ""; if (dict.ContainsKey(tabs[j].Name)) { itemVal = dict[tabs[j].Name]?.ToString(); } if (tabs[j].Name== "RunWhetherNormal") { cels[j] = itemVal?.ToLower() == "true" ? "异常" : "正常"; } else if (tabs[j].Name == "Date") { cels[j] = Convert.ToDateTime(itemVal).ToString("yyyy-MM-dd"); } else { if (DateTime.TryParse(itemVal, out DateTime dt) && itemVal.Length >= 10) { cels[j] = dt.ToString("yyyy-MM-dd HH:mm:ss"); } else { cels[j] = itemVal; } } } } list.Add(cels); } pages = new PagesModel(list, searchModel); //var tabArray = tabs.Select(t => t.Label).ToArray(); List tabList = new List(); for (int i = 0; i < tabs.Count; i++) { var vals = tabs[i].Label.ToString(); var name = tabs[i].Name.ToString(); if (vals.Contains("@")) { string[] tmp = vals.Split('@');//按,分割字符串 var TableHeadeModel = tabList.Find(t => t.prop == tmp[0].ToString()); if (TableHeadeModel != null) { if (TableHeadeModel.children != null && TableHeadeModel.children.Count > 0) { TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null }); } else { TableHeadeModel.children = new List(); TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null }); } } else { var cc = new DevOpeAccountConfigTableHeadeModel { prop = tmp[0].ToString(), label = tmp[0].ToString(), Level = 1, children = new List() }; cc.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null }); tabList.Add(cc); } } else { tabList.Add(new DevOpeAccountConfigTableHeadeModel { prop = vals, label = vals, Level = 1, children = null }); } } var data = new { DevOpeAccountConfigCode = confCode, tabName = tabs.Select(t => t.Label).ToArray(), tabArray = tabList, pages }; return new ApiResult(data); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 查询运行台账-【返回配置集合】 /// /// /// [HttpPost("GetDevOpeAccountConfingAsync")] [AllowAnonymous] public async Task GetDevOpeAccountConfingAsync(TdevDevOpeAccountSearchModel searchModel) { if (searchModel == null) { return new ApiResult(ReturnCode.ArgsError); } try { PagesModel pages = null; List select = await _TdevDevOpeAccountService.GetDevOpeAccountConfigSelectAsync(searchModel); pages = new PagesModel(select, searchModel); var data = new { pages }; return new ApiResult(data); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 创建业主设备运行台账 /// /// /// [HttpPost("CreateDevOpeAccountAsync")] public async Task CreateDevOpeAccountAsync(TdevDevOpeAccountViewModel content) { if (content == null) { return new ApiResult(ReturnCode.ArgsError); } try { await _TdevDevOpeAccountService.CreateOneAsync(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 删除业主设备运行台账 /// /// /// [HttpDelete("DeleteDevOpeAccountAsync/{id}")] public async Task DeleteDevOpeAccountAsync(string id) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { await _TdevDevOpeAccountService.DeleteAsync(id); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 更新业主设备运行台账 /// /// /// /// [HttpPut("UpdateDevOpeAccountAsync/{id}")] public async Task UpdateDevOpeAccountAsync(string id, TdevDevOpeAccountUpdateModel updateModel) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { await _TdevDevOpeAccountService.UpdateAsync(id, updateModel); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 发送邮件(附件为台帐) /// /// /// [HttpPost("SendDevOpeAccountToEmail")] [AllowAnonymous] public async Task SendDevOpeAccountToEmail(SendDevOpeAccountToEmailModel searchModel) { if (searchModel == null) { return; } try { var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel); IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet(); #region //标题加粗居中. IFont font = workbook.CreateFont(); font.IsBold = true; font.FontHeightInPoints = 25; font.FontName = "宋体"; ICellStyle titleCellStyle = workbook.CreateCellStyle(); titleCellStyle.SetFont(font); titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中 //边框 titleCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; titleCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; titleCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; titleCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; titleCellStyle.WrapText = true; #endregion #region //标题加粗居中. IFont font1 = workbook.CreateFont(); font1.IsBold = true; font1.FontHeightInPoints = 14; font1.FontName = "宋体"; ICellStyle headCellStyle = workbook.CreateCellStyle(); headCellStyle.SetFont(font1); headCellStyle.Alignment = HorizontalAlignment.Center; //字体左右居中 headCellStyle.VerticalAlignment = VerticalAlignment.Center;//字体上下居中 //边框 headCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; headCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; headCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; headCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; headCellStyle.WrapText = true; #endregion #region //标题加粗居中. IFont font2 = workbook.CreateFont(); font2.IsBold = false; font2.FontHeightInPoints = 12; font2.FontName = "宋体"; ICellStyle rowCellStyle = workbook.CreateCellStyle(); rowCellStyle.SetFont(font2); rowCellStyle.Alignment = HorizontalAlignment.Center; //字体居中 //边框 rowCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; rowCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; rowCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; rowCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; rowCellStyle.WrapText = true; #endregion IRow title = sheet.CreateRow(0); title.Height = 30 * 25; title.CreateCell(0).SetCellValue("VOCs治理设施基本信息与维保管理信息表"); foreach (var item in title.Cells) { item.CellStyle = titleCellStyle; } if (devOpeAccountList == null || devOpeAccountList.Count() == 0) { IRow content1 = sheet.CreateRow(1); content1.CreateCell(0).SetCellValue("暂无数据"); content1.Cells[0].CellStyle = headCellStyle; sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 15)); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 15)); } else { List tabs = new List(); var confCode = searchModel.C_DevOpeAccountConfigCode; IRow head = sheet.CreateRow(1); IRow head2 = sheet.CreateRow(2); IRow head3 = sheet.CreateRow(3); head.Height = 30 * 20; head2.Height = 30 * 20; head3.Height = 30 * 20; if (!string.IsNullOrEmpty(confCode)) { head.CreateCell(0).SetCellValue("日期"); tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date", BReadDevSpot = false }); sheet.AddMergedRegion(new CellRangeAddress(1, 3, 0, 0)); int cel = 1; var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode); if (content != null && !string.IsNullOrEmpty(content.C_Config)) { var RunSpotConfig = JsonConvert.DeserializeObject(content.C_Config); List CtabColumns = new List(); int headerNum = 1; foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList()) { if (item != null) { headerNum = item.Label.Split("@").Count(); if (item.BReadDevSpot) { headerNum++; tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot }); tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot }); tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot }); CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot }); CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot }); CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot }); } else { tabs.Add(item); CtabColumns.Add(item); } } } bool IsVal = false; int merge = 0; bool IsVal2 = false; int merge2 = 0; foreach (var tab in CtabColumns) { if (tab != null) { var vals = tab.Label; if (vals.Contains("@")) { string[] tmp = vals.Split('@');//按,分割字符串 if (tmp.Count() == 3) { var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString(); var getCel2 = sheet.GetRow(2).GetCell(cel - 1 - merge2).ToString(); if (getCel == tmp[0].ToString()) { head.CreateCell(cel).SetCellValue(""); merge++; IsVal = true; } else { IsVal = false; head.CreateCell(cel).SetCellValue(tmp[0].ToString()); } if (getCel2 == tmp[1].ToString()) { head2.CreateCell(cel).SetCellValue(""); merge2++; IsVal2 = true; } else { head2.CreateCell(cel).SetCellValue(tmp[1].ToString()); IsVal2 = false; } head3.CreateCell(cel).SetCellValue(tmp[2].ToString()); } else { IsVal2 = false; var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString(); if (getCel == tmp[0].ToString()) { head.CreateCell(cel).SetCellValue(""); merge++; IsVal = true; } else { IsVal = false; head.CreateCell(cel).SetCellValue(tmp[0].ToString()); } head2.CreateCell(cel).SetCellValue(tmp[1].ToString()); head3.CreateCell(cel).SetCellValue(""); sheet.AddMergedRegion(new CellRangeAddress(2, 3, cel, cel)); } } else { IsVal = false; IsVal2 = false; head.CreateCell(cel).SetCellValue(vals); head2.CreateCell(cel).SetCellValue(""); head3.CreateCell(cel).SetCellValue(""); sheet.AddMergedRegion(new CellRangeAddress(1, 3, cel, cel)); } if (!IsVal && merge > 0) { sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1)); merge = 0; } if (!IsVal2 && merge2 > 0) { sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1)); merge2 = 0; } cel++; } } if (cel == CtabColumns.Count + 1 && merge > 0 && IsVal) { sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1)); merge = 0; IsVal = false; } if (cel == CtabColumns.Count + 1 && merge2 > 0 && IsVal2) { sheet.AddMergedRegion(new CellRangeAddress(1, 2, cel - merge2 - 1, cel - 1)); merge2 = 0; IsVal2 = false; } } } foreach (var item in head.Cells) { item.CellStyle = headCellStyle; } foreach (var item in head2.Cells) { item.CellStyle = headCellStyle; } foreach (var item in head3.Cells) { item.CellStyle = headCellStyle; } int rowNumber = 4; var confData = devOpeAccountList.Select(t => t.C_Content).ToList(); for (int i = 0; i < confData.Count; i++) { IRow content = sheet.CreateRow(rowNumber); Dictionary dict = JsonConvert.DeserializeObject>(confData[i]); int forCount = dict.Count + 1; if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; } for (int j = 0; j < forCount; j++) { if (j < tabs.Count) { string celVal = ""; if (dict.ContainsKey(tabs[j].Name)) { celVal = dict[tabs[j].Name]?.ToString(); } if (tabs[j].Name == "RunWhetherNormal") { celVal = celVal.ToLower() == "true" ? "异常" : "正常"; content.CreateCell(j).SetCellValue(celVal); } else if (tabs[j].Name == "Date") { content.CreateCell(j).SetCellValue(Convert.ToDateTime(celVal).ToString("yyyy-MM-dd")); } else if (DateTime.TryParse(celVal, out DateTime dt) && celVal.Length >= 10) { content.CreateCell(j).SetCellValue(dt.ToString("yyyy-MM-dd HH:mm:ss")); } else { content.CreateCell(j).SetCellValue(celVal); } } } foreach (var item in content.Cells) { item.CellStyle = rowCellStyle; } rowNumber++; } sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, tabs.Count - 1)); for (int i = 0; i <= tabs.Count; i++) { sheet.SetColumnWidth(i, 5000); } } using (var stream = new NpoiMemoryStream()) { workbook.Write(stream); stream.Seek(0, SeekOrigin.Begin); string emailName = $"{searchModel.DevName} 设备台账记录报表 {searchModel.D_Start?.ToString("yyyy-MM-dd HH:mm:ss")}至{searchModel.D_End?.ToString("yyyy-MM-dd HH:mm:ss")}"; EmailHelper.SendEmail(searchModel.Mails, emailName, "", "报表见附件", $"{emailName}.xlsx", "application/vnd.ms-excel", stream); } } catch (Exception ex) { throw; } } /// /// 查询运行台账-导出-Excel /// /// /// [HttpPost("DevOpeAccountContentExportExcel")] [AllowAnonymous] public async Task DevOpeAccountContentExportExcel(TdevDevOpeAccountSearchModel searchModel) { try { var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel); IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet(); #region //标题加粗居中. IFont font = workbook.CreateFont(); font.IsBold = true; font.FontHeightInPoints = 25; font.FontName = "宋体"; ICellStyle titleCellStyle = workbook.CreateCellStyle(); titleCellStyle.SetFont(font); titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中 //边框 titleCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; titleCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; titleCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; titleCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; titleCellStyle.WrapText = true; #endregion #region //标题加粗居中. IFont font1 = workbook.CreateFont(); font1.IsBold = true; font1.FontHeightInPoints = 14; font1.FontName = "宋体"; ICellStyle headCellStyle = workbook.CreateCellStyle(); headCellStyle.SetFont(font1); headCellStyle.Alignment = HorizontalAlignment.Center; //字体左右居中 headCellStyle.VerticalAlignment = VerticalAlignment.Center;//字体上下居中 //边框 headCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; headCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; headCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; headCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; headCellStyle.WrapText = true; #endregion #region //标题加粗居中. IFont font2 = workbook.CreateFont(); font2.IsBold = false; font2.FontHeightInPoints = 12; font2.FontName = "宋体"; ICellStyle rowCellStyle = workbook.CreateCellStyle(); rowCellStyle.SetFont(font2); rowCellStyle.Alignment = HorizontalAlignment.Center; //字体居中 //边框 rowCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; rowCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; rowCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; rowCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; rowCellStyle.WrapText = true; #endregion IRow title = sheet.CreateRow(0); title.Height = 30 * 25; title.CreateCell(0).SetCellValue("VOCs治理设施基本信息与维保管理信息表"); foreach (var item in title.Cells) { item.CellStyle = titleCellStyle; } if (devOpeAccountList == null || devOpeAccountList.Count() == 0) { IRow content1 = sheet.CreateRow(1); content1.CreateCell(0).SetCellValue("暂无数据"); content1.Cells[0].CellStyle = headCellStyle; sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 15)); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 15)); } else { List tabs = new List(); var confCode = searchModel.C_DevOpeAccountConfigCode; IRow head = sheet.CreateRow(1); IRow head2 = sheet.CreateRow(2); IRow head3 = sheet.CreateRow(3); head.Height = 30 * 20; head2.Height = 30 * 20; head3.Height = 30 * 20; if (!string.IsNullOrEmpty(confCode)) { head.CreateCell(0).SetCellValue("日期"); tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date",BReadDevSpot=false }); sheet.AddMergedRegion(new CellRangeAddress(1, 3, 0, 0)); int cel = 1; var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode); if (content != null && !string.IsNullOrEmpty(content.C_Config)) { var RunSpotConfig = JsonConvert.DeserializeObject(content.C_Config); List CtabColumns = new List(); int headerNum = 1; foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList()) { if (item != null) { headerNum= item.Label.Split("@").Count(); if (item.BReadDevSpot) { headerNum++; tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot }); tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot }); tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot }); CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot }); CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot }); CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot }); } else { tabs.Add(item); CtabColumns.Add(item); } } } bool IsVal = false;int merge = 0; bool IsVal2 = false; int merge2 = 0; foreach (var tab in CtabColumns) { if (tab != null) { var vals = tab.Label; if (vals.Contains("@")) { string[] tmp = vals.Split('@');//按,分割字符串 if (tmp.Count()==3) { var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString(); var getCel2 = sheet.GetRow(2).GetCell(cel - 1 - merge2).ToString(); if (getCel == tmp[0].ToString()) { head.CreateCell(cel).SetCellValue(""); merge++; IsVal = true; } else { IsVal = false; head.CreateCell(cel).SetCellValue(tmp[0].ToString()); } if (getCel2 == tmp[1].ToString()) { head2.CreateCell(cel).SetCellValue(""); merge2++; IsVal2 = true; } else { head2.CreateCell(cel).SetCellValue(tmp[1].ToString()); IsVal2 = false; } head3.CreateCell(cel).SetCellValue(tmp[2].ToString()); } else { IsVal2 = false; var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString(); if (getCel == tmp[0].ToString()) { head.CreateCell(cel).SetCellValue(""); merge++; IsVal = true; } else { IsVal = false; head.CreateCell(cel).SetCellValue(tmp[0].ToString()); } head2.CreateCell(cel).SetCellValue(tmp[1].ToString()); head3.CreateCell(cel).SetCellValue(""); sheet.AddMergedRegion(new CellRangeAddress(2, 3, cel, cel)); } } else { IsVal = false; IsVal2 = false; head.CreateCell(cel).SetCellValue(vals); head2.CreateCell(cel).SetCellValue(""); head3.CreateCell(cel).SetCellValue(""); sheet.AddMergedRegion(new CellRangeAddress(1, 3, cel, cel)); } if (!IsVal && merge > 0) { sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1)); merge = 0; } if (!IsVal2 && merge2 > 0) { sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1)); merge2 = 0; } cel++; } } if (cel== CtabColumns.Count+1&& merge>0&& IsVal) { sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1)); merge = 0; IsVal = false; } if (cel == CtabColumns.Count + 1 && merge2 > 0 && IsVal2) { sheet.AddMergedRegion(new CellRangeAddress(1, 2, cel - merge2 - 1, cel - 1)); merge2 = 0; IsVal2 = false; } } } foreach (var item in head.Cells) { item.CellStyle = headCellStyle; } foreach (var item in head2.Cells) { item.CellStyle = headCellStyle; } foreach (var item in head3.Cells) { item.CellStyle = headCellStyle; } int rowNumber = 4; var confData = devOpeAccountList.Select(t => t.C_Content).ToList(); for (int i = 0; i < confData.Count; i++) { IRow content = sheet.CreateRow(rowNumber); Dictionary dict = JsonConvert.DeserializeObject>(confData[i]); int forCount = dict.Count + 1; if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; } for (int j = 0; j < forCount; j++) { if (j < tabs.Count) { string celVal = ""; if (dict.ContainsKey(tabs[j].Name)) { celVal = dict[tabs[j].Name]?.ToString(); } if (tabs[j].Name == "RunWhetherNormal") { celVal = celVal.ToLower() == "true" ? "异常" : "正常"; content.CreateCell(j).SetCellValue(celVal); }else if (tabs[j].Name == "Date") { content.CreateCell(j).SetCellValue(Convert.ToDateTime(celVal).ToString("yyyy-MM-dd")); } else if(DateTime.TryParse(celVal, out DateTime dt) && celVal.Length >= 10) { content.CreateCell(j).SetCellValue(dt.ToString("yyyy-MM-dd HH:mm:ss")); } else { content.CreateCell(j).SetCellValue(celVal); } } } foreach (var item in content.Cells) { item.CellStyle = rowCellStyle; } rowNumber++; } sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, tabs.Count-1)); for (int i = 0; i <= tabs.Count; i++) { sheet.SetColumnWidth(i, 5000); } } using (MemoryStream stream = new MemoryStream()) { workbook.Write(stream); byte[] data = stream.ToArray(); return File(data, "application/vnd.ms-excel", "设备运行台账.xlsx"); } } catch (Exception ex) { throw; } } } }