[後端] 修改獨立棟別以sheet區分

This commit is contained in:
dev02 2023-06-27 10:53:38 +08:00
parent 008e10c439
commit 518a97e461

View File

@ -418,10 +418,8 @@ namespace FrontendWebApi.ApiControllers
} }
List<Dictionary<string, byte[]>> docFile = new List<Dictionary<string, byte[]>>(); List<Dictionary<string, byte[]>> docFile = new List<Dictionary<string, byte[]>>();
foreach (var r in result) var workbook = new XSSFWorkbook();
{ #region excel設定
var workbook = new XSSFWorkbook();
#region excel設定
IFont font12 = workbook.CreateFont(); IFont font12 = workbook.CreateFont();
font12.FontName = "新細明體"; font12.FontName = "新細明體";
font12.FontHeightInPoints = 12; font12.FontHeightInPoints = 12;
@ -462,6 +460,8 @@ namespace FrontendWebApi.ApiControllers
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true; stylein12.WrapText = true;
#endregion #endregion
foreach (var r in result)
{
string buildingName = r.Select(x => x.building_name).FirstOrDefault(); string buildingName = r.Select(x => x.building_name).FirstOrDefault();
var sheet = workbook.CreateSheet($"{buildingName}電表報表"); var sheet = workbook.CreateSheet($"{buildingName}電表報表");
int RowPosition = 0; int RowPosition = 0;
@ -551,26 +551,17 @@ namespace FrontendWebApi.ApiControllers
} }
} }
} }
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
docFile.Add(new Dictionary<string, byte[]>() { { $"{buildingName}電表報表.xlsx", ms.ToArray() } });
} }
var zipMs = new MemoryStream(); var ms = new NpoiMemoryStream
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(System.Text.Encoding.UTF8))
{ {
foreach(var d in docFile) AllowClose = false
{ };
foreach(var dic in d) workbook.Write(ms);
{ ms.Flush();
zip.AddEntry(dic.Key, dic.Value); ms.Seek(0, SeekOrigin.Begin);
}
}
zip.Save(zipMs);
}
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
return File(zipMs.ToArray(), "application/octet-stream", "電表報表.zip"); return File(ms, "application/vnd.ms", "電表報表.xlsx");
} }
[HttpPost] [HttpPost]
@ -611,54 +602,54 @@ namespace FrontendWebApi.ApiControllers
} }
} }
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
foreach (var b in building) foreach (var b in building)
{ {
var res1 = result1.Where(x => x.Select(x => x.building_tag).Contains(b.building_tag)).ToList(); var res1 = result1.Where(x => x.Select(x => x.building_tag).Contains(b.building_tag)).ToList();
var res2 = result2.Where(x => x.Select(x => x.building_tag).Contains(b.building_tag)).ToList(); var res2 = result2.Where(x => x.Select(x => x.building_tag).Contains(b.building_tag)).ToList();
if (res1.Count > 0 || res2.Count > 0) if (res1.Count > 0 || res2.Count > 0)
{ {
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($"{b.full_name}電表報表"); var sheet = workbook.CreateSheet($"{b.full_name}電表報表");
int RowPosition = 0; int RowPosition = 0;
@ -793,27 +784,18 @@ namespace FrontendWebApi.ApiControllers
cell.CellStyle = style12; cell.CellStyle = style12;
} }
} }
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
docFile.Add(new Dictionary<string, byte[]>() { { $"{b.full_name}電表報表.xlsx", ms.ToArray() } });
} }
} }
var zipMs = new MemoryStream(); var ms = new NpoiMemoryStream
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(System.Text.Encoding.UTF8))
{ {
foreach (var d in docFile) AllowClose = false
{ };
foreach (var dic in d) workbook.Write(ms);
{ ms.Flush();
zip.AddEntry(dic.Key, dic.Value); ms.Seek(0, SeekOrigin.Begin);
}
}
zip.Save(zipMs);
}
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
return File(zipMs.ToArray(), "application/octet-stream", "電表報表.zip"); return File(ms, "application/vnd.ms-excel", "電表報表.xlsx");
} }
public static int GetDayInMonth(string yearMonth) public static int GetDayInMonth(string yearMonth)