fix timestamp report #1843 (#1844)
Some checks failed
release-dev / release-dev (push) Failing after 12s

Co-authored-by: harid <harid_pr61@live.rmutl.com>
This commit is contained in:
Harid Promsri 2025-10-07 13:17:58 +07:00 committed by GitHub
parent 1ec8e51c37
commit 7ddc1debfc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 38 deletions

View file

@ -2168,12 +2168,15 @@ namespace BMA.EHR.Leave.Service.Controllers
var templatePath = Path.Combine(_hostingEnvironment.ContentRootPath, "Reports", "TimeStampRecords.xlsx"); var templatePath = Path.Combine(_hostingEnvironment.ContentRootPath, "Reports", "TimeStampRecords.xlsx");
byte[] templateBytes = System.IO.File.ReadAllBytes(templatePath); byte[] templateBytes = System.IO.File.ReadAllBytes(templatePath);
//using (var package = new ExcelPackage(fileInfo))
using (var stream = new MemoryStream(templateBytes)) using (var stream = new MemoryStream(templateBytes))
using (var package = new ExcelPackage(stream)) using (var package = new ExcelPackage(stream))
{ {
//var worksheet = package.Workbook.Worksheets.Add("Sheet1");
var worksheet = package.Workbook.Worksheets["Sheet1"] ?? package.Workbook.Worksheets[0]; var worksheet = package.Workbook.Worksheets["Sheet1"] ?? package.Workbook.Worksheets[0];
// กำหนดให้ใช้ฟอนต์ TH SarabunPSK ซึ่งเป็นฟอนต์มาตรฐานราชการไทย
// **ข้อควรระวัง:** หากเครื่องผู้ใช้ไม่มีฟอนต์นี้ติดตั้งอยู่ การจัดหน้าเอกสารจะเพี้ยน
worksheet.Cells.Style.Font.Name = "TH SarabunPSK";
worksheet.Cells["A1:J1"].Merge = true; worksheet.Cells["A1:J1"].Merge = true;
worksheet.Cells["A2:J2"].Merge = true; worksheet.Cells["A2:J2"].Merge = true;
@ -2185,56 +2188,50 @@ namespace BMA.EHR.Leave.Service.Controllers
range.Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center; range.Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
range.Style.Font.Bold = true; range.Style.Font.Bold = true;
} }
//worksheet.Cells[1, 1].Value = "แบบการลงเวลาปฏิบัติราชการ";
worksheet.Cells[2, 1].Value = organizationName; worksheet.Cells[2, 1].Value = organizationName;
worksheet.Cells[3, 1].Value = dateTimeStamp; worksheet.Cells[3, 1].Value = dateTimeStamp;
using (var range = worksheet.Cells[4, 1, 4, 10])
{
range.Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
range.Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
range.Style.Font.Bold = true;
}
//worksheet.Cells[4, 1].Value = "ลำดับที่";
//worksheet.Cells[4, 2].Value = "ชื่อ - สกุล";
//worksheet.Cells[4, 3].Value = "รอบ";
//worksheet.Cells[4, 4].Value = "วันที่เข้างาน";
//worksheet.Cells[4, 5].Value = "พิกัด";
//worksheet.Cells[4, 6].Value = "เวลามา";
//worksheet.Cells[4, 7].Value = "วันที่ออกงาน";
//worksheet.Cells[4, 8].Value = "พิกัด";
//worksheet.Cells[4, 9].Value = "เวลากลับ";
//worksheet.Cells[4, 10].Value = "หมายเหตุ";
int startRow = 5; int startRow = 5;
foreach (var emp in employees) int colCount = 10;
int totalRows = employees.Count;
// เตรียม List<object[]> สำหรับใช้กับ LoadFromArrays
var data = new List<object[]>(totalRows);
for (int i = 0; i < totalRows; i++)
{ {
worksheet.Cells[startRow, 1].Value = emp.no; var emp = employees[i];
worksheet.Cells[startRow, 2].Value = emp.fullName; data.Add(new object[]
worksheet.Cells[startRow, 3].Value = emp.dutyTimeName; {
worksheet.Cells[startRow, 4].Value = emp.checkInDate; emp.no,
worksheet.Cells[startRow, 5].Value = emp.checkInLocation; emp.fullName,
worksheet.Cells[startRow, 6].Value = emp.checkInTime; emp.dutyTimeName,
worksheet.Cells[startRow, 7].Value = emp.checkedOutDate; emp.checkInDate,
worksheet.Cells[startRow, 8].Value = emp.checkOutLocation; emp.checkInLocation,
worksheet.Cells[startRow, 9].Value = emp.checkOutTime; emp.checkInTime,
worksheet.Cells[startRow, 10].Value = emp.remark; emp.checkedOutDate,
startRow++; emp.checkOutLocation,
emp.checkOutTime,
emp.remark
});
} }
// ใส่กรอบให้ตาราง // เขียนข้อมูลลง Excel ครั้งเดียว
using (var range = worksheet.Cells[5, 1, startRow - 1, 10]) worksheet.Cells[startRow, 1].LoadFromArrays(data);
// กำหนดสไตล์ตัวบาง + ขอบ
using (var range = worksheet.Cells[startRow, 1, startRow + totalRows - 1, colCount])
{ {
range.Style.Font.Bold = false;
range.Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin; range.Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin; range.Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin; range.Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin; range.Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
} }
int lastRow = startRow + 2; // ส่วนสรุปท้ายตาราง
int lastRow = startRow + totalRows + 2;
worksheet.Cells[lastRow, 2].Value = type.Trim().ToUpper() == "OFFICER" ? "ข้าราชการทั้งหมด" : "ลูกจ้างประจำทั้งหมด"; worksheet.Cells[lastRow, 2].Value = type.Trim().ToUpper() == "OFFICER" ? "ข้าราชการทั้งหมด" : "ลูกจ้างประจำทั้งหมด";
worksheet.Cells[lastRow, 5].Value = profile?.Count; worksheet.Cells[lastRow, 5].Value = profile?.Count;
worksheet.Cells[lastRow, 6].Value = "คน"; worksheet.Cells[lastRow, 6].Value = "คน";
@ -2260,6 +2257,8 @@ namespace BMA.EHR.Leave.Service.Controllers
worksheet.Cells[lastRow + 6, 2].Value = "เรียน"; worksheet.Cells[lastRow + 6, 2].Value = "เรียน";
worksheet.Cells[lastRow + 7, 2].Value = "เพื่อโปรดทราบ"; worksheet.Cells[lastRow + 7, 2].Value = "เพื่อโปรดทราบ";
worksheet.Cells[lastRow + 7, 9].Value = "ทราบ"; worksheet.Cells[lastRow + 7, 9].Value = "ทราบ";
worksheet.Cells[lastRow + 7, 9].Style.Font.Bold = true;
worksheet.Cells[lastRow + 7, 9].Style.Font.Size = 22;
worksheet.Cells[lastRow + 8, 2].Value = "................................"; worksheet.Cells[lastRow + 8, 2].Value = "................................";
worksheet.Cells[lastRow + 8, 9].Value = "................................"; worksheet.Cells[lastRow + 8, 9].Value = "................................";
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(); worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();