[後端] 完成水電表匯出

This commit is contained in:
dev02 2023-05-15 12:04:42 +08:00
parent 61c0f90979
commit f99a583fea

View File

@ -297,7 +297,7 @@ namespace FrontendWebApi.ApiControllers
[HttpPost] [HttpPost]
[Route("api/ExportElectricList")] [Route("api/ExportElectricList")]
public FileResult OpeExportExcel([FromBody] HydroMeterInput input) public FileResult OpeExportExcelElec([FromBody] HydroMeterInput input)
{ {
var result = this.ElectricList(input).Result.Value.Data.ToList(); var result = this.ElectricList(input).Result.Value.Data.ToList();
@ -365,14 +365,11 @@ namespace FrontendWebApi.ApiControllers
cell.SetCellValue("設備"); cell.SetCellValue("設備");
cell.CellStyle = styleLine12; 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 = row.CreateCell(i++); cell.CellStyle = styleLine12;
cell.SetCellValue(rr.timeStamp);
cell.CellStyle = styleLine12;
}
} }
cell = row.CreateCell(i++); cell = row.CreateCell(i++);
@ -389,6 +386,7 @@ namespace FrontendWebApi.ApiControllers
foreach (var r in result) foreach (var r in result)
{ {
RowPosition += 1; RowPosition += 1;
int k = 3;
row = sheet.CreateRow(RowPosition); row = sheet.CreateRow(RowPosition);
for (int j = 0; j <= i; j++) for (int j = 0; j <= i; j++)
{ {
@ -406,27 +404,26 @@ namespace FrontendWebApi.ApiControllers
cell.SetCellValue(r.device_serial_tag); cell.SetCellValue(r.device_serial_tag);
} }
int k = 3;
if (j == 3) if (j == 3)
{ {
foreach (var rr in r.rawData) foreach (var rr in r.rawData)
{ {
if (j++ == k++) cell.SetCellValue(rr.avg_rawdata.ToString());
{ j++;
cell.SetCellValue(rr.avg_rawdata.ToString()); k++;
} cell = row.CreateCell(j);
} }
} }
if (j == k++) if (j == k)
{ {
cell.SetCellValue(r.total); cell.SetCellValue(r.total);
} }
if (j == k++) if (j == k+1)
{ {
cell.SetCellValue(r.price); cell.SetCellValue(r.price);
} }
if (j == k++) if (j == k+2)
{ {
cell.SetCellValue(r.total_price); cell.SetCellValue(r.total_price);
} }
@ -446,5 +443,154 @@ namespace FrontendWebApi.ApiControllers
return File(ms, "application/vnd.ms-excel", "電表報表.xlsx"); 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");
}
} }
} }