diff --git a/FrontendWebApi/ApiControllers/HydroMeterController.cs b/FrontendWebApi/ApiControllers/HydroMeterController.cs index c195bfa..4d1e764 100644 --- a/FrontendWebApi/ApiControllers/HydroMeterController.cs +++ b/FrontendWebApi/ApiControllers/HydroMeterController.cs @@ -833,8 +833,6 @@ namespace FrontendWebApi.ApiControllers result.Add(ElectricListEachTotal(input).Result.Value.Data.ToList()); - // --- 8/24 修改到這裡囉 --- - List> docFile = new List>(); var workbook = new XSSFWorkbook(); @@ -879,106 +877,60 @@ namespace FrontendWebApi.ApiControllers stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; stylein12.WrapText = true; #endregion - foreach (var r in result) + + using (FileStream templateStream = new FileStream(".\\template\\ElectricEachTotalCompare_temp.xlsx", FileMode.Open, FileAccess.Read)) { - if (r.Count > 0) + var templateWorkbook = new XSSFWorkbook(templateStream); + var sheet = templateWorkbook.GetSheetAt(0); // 這裡假設您要使用第一個工作表 + + // 設定要填入資料的位置(rowIndex 和 columnIndex) + int rowIndex = 4; // 假設要從第二列開始填入 + int columnIndex = 1; // 假設第一欄是要填入的位置 + + foreach (var r in result) { + columnIndex = 1; + IRow row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex); + string buildingName = r.Select(x => x.building_name).FirstOrDefault(); - var sheet = workbook.CreateSheet($"{buildingName} 電表報表"); - 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); + string floorTag = r.Select(x => x.floor_tag).FirstOrDefault(); + ICell cell = row.GetCell(columnIndex) ?? row.CreateCell(columnIndex); + cell.SetCellValue(buildingName + floorTag); - int i = 0; - ICell cell = row.CreateCell(i++); - cell.SetCellValue("棟別"); - cell.CellStyle = styleLine12; - cell = row.CreateCell(i++); - cell.SetCellValue("樓層"); - cell.CellStyle = styleLine12; - cell = row.CreateCell(i++); - cell.SetCellValue("設備"); - cell.CellStyle = styleLine12; - cell = row.CreateCell(i++); - cell.SetCellValue("單位"); - cell.CellStyle = styleLine12; + cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++); + string deviceFullName = r.Select(x => x.device_full_name).FirstOrDefault(); + cell.SetCellValue(deviceFullName); - foreach (var rr in r.FirstOrDefault().rawData) + foreach (var rawD in r.FirstOrDefault().rawData) { - cell = row.CreateCell(i++); - cell.SetCellValue(rr.timeStamp); - cell.CellStyle = styleLine12; + cell = row.GetCell(columnIndex++) ?? row.CreateCell(columnIndex++); + cell.SetCellValue(rawD.avg_rawdata); } - cell = row.CreateCell(i++); - cell.SetCellValue("小計"); - cell.CellStyle = styleLine12; - cell = row.CreateCell(i++); - cell.SetCellValue("單價"); - cell.CellStyle = styleLine12; - cell = row.CreateCell(i++); - cell.SetCellValue("金額總計"); - cell.CellStyle = styleLine12; - #endregion - - foreach (var rr in r) - { - RowPosition += 1; - int k = 4; - row = sheet.CreateRow(RowPosition); - for (int j = 0; j <= i; j++) - { - cell = row.CreateCell(j); - if (j == 0) - { - cell.SetCellValue(rr.building_name); - } - else if (j == 1) - { - cell.SetCellValue(rr.floor_tag); - } - else if (j == 2) - { - cell.SetCellValue(rr.device_full_name); - } - else if (j == 3) - { - cell.SetCellValue("kWh"); - } - - else if (j == 4) - { - foreach (var rrr in rr.rawData) - { - cell.SetCellValue(rrr.avg_rawdata.ToString()); - j++; - k++; - cell = row.CreateCell(j); - } - } - - if (j == k) - { - cell.SetCellValue(rr.total); - } - else if (j == k + 1) - { - cell.SetCellValue(rr.price); - } - else if (j == k + 2) - { - cell.SetCellValue(rr.total_price); - } - - cell.CellStyle = style12; - } - } + rowIndex++; } + + // // 設定欲填入的值 + // string buildingName = "Building A"; + // string floorTag = "1F"; + // string deviceFullName = "Device X"; + // string unit = "kWh"; + + // // 填入資料 + // IRow row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex); + // ICell cell = row.GetCell(columnIndex) ?? row.CreateCell(columnIndex); + // cell.SetCellValue(buildingName); + + // // 同樣方式填入其他資料 + // cell = row.GetCell(columnIndex + 1) ?? row.CreateCell(columnIndex + 1); + // cell.SetCellValue(floorTag); + + // ... + } + // --- 8/24 修改到這裡囉 --- + var ms = new NpoiMemoryStream { AllowClose = false @@ -1062,7 +1014,7 @@ left join (select device_number, sum(kwh_result) kwh_sum from archive_electric_m left join (select device_number, sum(kwh_result) kwh_sum from archive_electric_meter_month where year(start_timestamp) = {last_year} and month(start_timestamp) = {start_month} group by device_number) d on a.device_number COLLATE utf8mb4_0900_ai_ci = d.device_number -- last Month join building e on a.device_building_tag = e.building_tag where {sqlWhere} -order by a.priority"; +order by e.priority, a.priority"; Logger.LogInformation("SQL = " + sql + " building=" + input.building_tag + " floor_tag = " + input.floor_tag); var rawData = await backendRepository.GetAllAsync(sql); diff --git a/FrontendWebApi/template/ElectricEachTotalCompare_temp.xlsx b/FrontendWebApi/template/ElectricEachTotalCompare_temp.xlsx new file mode 100644 index 0000000..29bb6a6 Binary files /dev/null and b/FrontendWebApi/template/ElectricEachTotalCompare_temp.xlsx differ