From 7cc15613bbf35201205598d98f0deb81b52a015d Mon Sep 17 00:00:00 2001 From: jiahao Date: Fri, 21 Jul 2023 10:56:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AFAPI=EF=BC=9AHistory=20?= =?UTF-8?q?=E5=8C=AF=E5=87=BA=20Excel=20=E5=90=84=E5=88=86=E9=A0=81?= =?UTF-8?q?=E8=B3=87=E6=96=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/HistoryController.cs | 78 +++++++++++++------ 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/FrontendWebApi/ApiControllers/HistoryController.cs b/FrontendWebApi/ApiControllers/HistoryController.cs index 0e847bc..a860c82 100644 --- a/FrontendWebApi/ApiControllers/HistoryController.cs +++ b/FrontendWebApi/ApiControllers/HistoryController.cs @@ -216,32 +216,66 @@ namespace FrontendWebApi.ApiControllers stylein12.WrapText = true; #endregion var data = he.ToList(); - ISheet sheet = workbook.CreateSheet($"{building.Where(x => x.building_tag == lhe.device_number.Split("_")[1]).Select(x => x.full_name).FirstOrDefault()}歷史資料"); - int RowPosition = 0; - #region set cell - IRow row = sheet.CreateRow(RowPosition); - sheet.SetColumnWidth(0, 4 * 160 * 12); - sheet.SetColumnWidth(1, 4 * 160 * 12); - sheet.SetColumnWidth(2, 4 * 160 * 12); - sheet.SetColumnWidth(3, 4 * 160 * 12); - ICell cell = row.CreateCell(0); - cell.SetCellValue("類型"); - cell.CellStyle = styleLine12; - cell = row.CreateCell(1); - cell.SetCellValue("設備名稱"); - cell.CellStyle = styleLine12; - cell = row.CreateCell(2); - cell.SetCellValue("數值"); - cell = row.CreateCell(3); - cell.SetCellValue("記錄時間"); - cell.CellStyle = styleLine12; - #endregion - if (data.Count > 0) { + string lastDeviceItem = string.Empty; + int RowPosition = 0; + IRow row; + ISheet sheet; + #region set cell + sheet = workbook.CreateSheet($"{building.Where(x => x.building_tag == lhe.device_number.Split("_")[1]).Select(x => x.full_name).FirstOrDefault()}{"_" + data[0].type}"); + row = sheet.CreateRow(RowPosition); + sheet.SetColumnWidth(0, 4 * 160 * 12); + sheet.SetColumnWidth(1, 4 * 160 * 12); + sheet.SetColumnWidth(2, 4 * 160 * 12); + sheet.SetColumnWidth(3, 4 * 160 * 12); + ICell cell = row.CreateCell(0); + cell.SetCellValue("類型"); + cell.CellStyle = styleLine12; + cell = row.CreateCell(1); + cell.SetCellValue("設備名稱"); + cell.CellStyle = styleLine12; + cell = row.CreateCell(2); + cell.SetCellValue("數值"); + cell = row.CreateCell(3); + cell.SetCellValue("記錄時間"); + cell.CellStyle = styleLine12; + #endregion + foreach (var d in data) { - RowPosition += 1; + if (RowPosition == 0 && lastDeviceItem == "") + { + lastDeviceItem = d.type; //第一次不用建立 sheet; + } + if (d.type != lastDeviceItem) + { + lastDeviceItem = d.type; + sheet = workbook.CreateSheet($"{building.Where(x => x.building_tag == lhe.device_number.Split("_")[1]).Select(x => x.full_name).FirstOrDefault()}{"_" + d.type}"); + #region set cell + row = sheet.CreateRow(RowPosition); + sheet.SetColumnWidth(0, 4 * 160 * 12); + sheet.SetColumnWidth(1, 4 * 160 * 12); + sheet.SetColumnWidth(2, 4 * 160 * 12); + sheet.SetColumnWidth(3, 4 * 160 * 12); + cell = row.CreateCell(0); + cell.SetCellValue("類型"); + cell.CellStyle = styleLine12; + cell = row.CreateCell(1); + cell.SetCellValue("設備名稱"); + cell.CellStyle = styleLine12; + cell = row.CreateCell(2); + cell.SetCellValue("數值"); + cell = row.CreateCell(3); + cell.SetCellValue("記錄時間"); + cell.CellStyle = styleLine12; + #endregion + RowPosition = 0; + } + else { + RowPosition += 1; + } + row = sheet.CreateRow(RowPosition); for (var i = 0; i < 4; i++) {