From f99a583fea29e31ca94e66c03ef5c9106eec9bad Mon Sep 17 00:00:00 2001 From: dev02 Date: Mon, 15 May 2023 12:04:42 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=BE=8C=E7=AB=AF]=20=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E6=B0=B4=E9=9B=BB=E8=A1=A8=E5=8C=AF=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/HydroMeterController.cs | 180 ++++++++++++++++-- 1 file changed, 163 insertions(+), 17 deletions(-) diff --git a/FrontendWebApi/ApiControllers/HydroMeterController.cs b/FrontendWebApi/ApiControllers/HydroMeterController.cs index 7369e71..9304660 100644 --- a/FrontendWebApi/ApiControllers/HydroMeterController.cs +++ b/FrontendWebApi/ApiControllers/HydroMeterController.cs @@ -297,7 +297,7 @@ namespace FrontendWebApi.ApiControllers [HttpPost] [Route("api/ExportElectricList")] - public FileResult OpeExportExcel([FromBody] HydroMeterInput input) + public FileResult OpeExportExcelElec([FromBody] HydroMeterInput input) { var result = this.ElectricList(input).Result.Value.Data.ToList(); @@ -365,14 +365,11 @@ namespace FrontendWebApi.ApiControllers cell.SetCellValue("設備"); cell.CellStyle = styleLine12; - foreach (var r in result) + foreach (var rr in result.FirstOrDefault().rawData) { - foreach (var rr in r.rawData) - { - cell = row.CreateCell(i++); - cell.SetCellValue(rr.timeStamp); - cell.CellStyle = styleLine12; - } + cell = row.CreateCell(i++); + cell.SetCellValue(rr.timeStamp); + cell.CellStyle = styleLine12; } cell = row.CreateCell(i++); @@ -389,6 +386,7 @@ namespace FrontendWebApi.ApiControllers foreach (var r in result) { RowPosition += 1; + int k = 3; row = sheet.CreateRow(RowPosition); for (int j = 0; j <= i; j++) { @@ -405,28 +403,27 @@ namespace FrontendWebApi.ApiControllers { cell.SetCellValue(r.device_serial_tag); } - - int k = 3; + if (j == 3) { foreach (var rr in r.rawData) { - if (j++ == k++) - { - cell.SetCellValue(rr.avg_rawdata.ToString()); - } + cell.SetCellValue(rr.avg_rawdata.ToString()); + j++; + k++; + cell = row.CreateCell(j); } } - if (j == k++) + if (j == k) { cell.SetCellValue(r.total); } - if (j == k++) + if (j == k+1) { cell.SetCellValue(r.price); } - if (j == k++) + if (j == k+2) { cell.SetCellValue(r.total_price); } @@ -446,5 +443,154 @@ namespace FrontendWebApi.ApiControllers return File(ms, "application/vnd.ms-excel", "電表報表.xlsx"); } + + [HttpGet] + [Route("api/ExportWaterList")] + public FileResult OpeExportExcelWater([FromBody] HydroMeterInput input) + { + var result = this.WaterList(input).Result.Value.Data.ToList(); + + var workbook = new XSSFWorkbook(); + #region excel設定 + IFont font12 = workbook.CreateFont(); + font12.FontName = "新細明體"; + font12.FontHeightInPoints = 12; + ICellStyle style12 = workbook.CreateCellStyle(); + style12.SetFont(font12); + style12.Alignment = HorizontalAlignment.Center; + style12.VerticalAlignment = VerticalAlignment.Center; + IFont font12Times = workbook.CreateFont(); + font12Times.FontName = "Times New Roman"; + font12Times.FontHeightInPoints = 12; + IFont font18 = workbook.CreateFont(); + font18.FontName = "新細明體"; + font18.FontHeightInPoints = 18; + font18.IsBold = true; + ICellStyle styleTitle18 = workbook.CreateCellStyle(); + styleTitle18.SetFont(font18); + styleTitle18.Alignment = HorizontalAlignment.Center; + styleTitle18.VerticalAlignment = VerticalAlignment.Center; + ICellStyle styleLeft12 = workbook.CreateCellStyle(); + styleLeft12.SetFont(font12); + styleLeft12.Alignment = HorizontalAlignment.Left; + styleLeft12.VerticalAlignment = VerticalAlignment.Center; + ICellStyle styleLine12 = workbook.CreateCellStyle(); + styleLine12.SetFont(font12); + styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; + styleLine12.VerticalAlignment = VerticalAlignment.Center; + styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; + styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; + styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; + styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; + ICellStyle stylein12 = workbook.CreateCellStyle(); + stylein12.SetFont(font12Times); + stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left; + stylein12.VerticalAlignment = VerticalAlignment.Center; + stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; + stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; + stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; + stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; + stylein12.WrapText = true; + #endregion + + var sheet = workbook.CreateSheet("電表報表"); + int RowPosition = 0; + if (result.Count > 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); + + 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; + + foreach (var rr in result.FirstOrDefault().rawData) + { + cell = row.CreateCell(i++); + cell.SetCellValue(rr.timeStamp); + 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; + #endregion + + foreach (var r in result) + { + RowPosition += 1; + int k = 3; + row = sheet.CreateRow(RowPosition); + for (int j = 0; j <= i; j++) + { + cell = row.CreateCell(j); + if (j == 0) + { + cell.SetCellValue(r.building_name); + } + if (j == 1) + { + cell.SetCellValue(r.floor_tag); + } + if (j == 2) + { + cell.SetCellValue(r.device_serial_tag); + } + + if (j == 3) + { + foreach (var rr in r.rawData) + { + cell.SetCellValue(rr.avg_rawdata.ToString()); + j++; + k++; + cell = row.CreateCell(j); + } + } + + if (j == k) + { + cell.SetCellValue(r.total); + } + if (j == k + 1) + { + cell.SetCellValue(r.price); + } + if (j == k + 2) + { + cell.SetCellValue(r.total_price); + } + + cell.CellStyle = style12; + } + } + } + + var ms = new NpoiMemoryStream + { + AllowClose = false + }; + workbook.Write(ms); + ms.Flush(); + ms.Seek(0, SeekOrigin.Begin); + + return File(ms, "application/vnd.ms-excel", "水表報表.xlsx"); + } } }