diff --git a/FrontendWebApi/ApiControllers/HydroMeterController.cs b/FrontendWebApi/ApiControllers/HydroMeterController.cs index 7275e6c..5bfda67 100644 --- a/FrontendWebApi/ApiControllers/HydroMeterController.cs +++ b/FrontendWebApi/ApiControllers/HydroMeterController.cs @@ -766,7 +766,157 @@ namespace FrontendWebApi.ApiControllers Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); return File(ms, "application/vnd.ms-excel", "電表報表.xlsx"); } + [HttpPost] + [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); + + Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); + + return File(ms, "application/vnd.ms-excel", "水表報表.xlsx"); + } public static int GetDayInMonth(string yearMonth) { List datesList = new List();