diff --git a/BMA.EHR.Leave/Controllers/LeaveReportController.cs b/BMA.EHR.Leave/Controllers/LeaveReportController.cs index b826077d..9a32e247 100644 --- a/BMA.EHR.Leave/Controllers/LeaveReportController.cs +++ b/BMA.EHR.Leave/Controllers/LeaveReportController.cs @@ -2133,52 +2133,64 @@ namespace BMA.EHR.Leave.Service.Controllers int colCount = 10; int totalRows = employees.Count; - // เตรียม List สำหรับใช้กับ LoadFromArrays - var data = new List(totalRows); - for (int i = 0; i < totalRows; i++) + // ตรวจสอบว่ามีข้อมูลหรือไม่ + if (totalRows > 0) { - var emp = employees[i]; - data.Add(new object[] + // เตรียม List สำหรับใช้กับ LoadFromArrays + var data = new List(totalRows); + for (int i = 0; i < totalRows; i++) { - emp.no, - emp.fullName, - emp.dutyTimeName, - emp.checkInDate, - emp.checkInLocation, - emp.checkInTime, - emp.checkedOutDate, - emp.checkOutLocation, - emp.checkOutTime, - emp.remark - }); + var emp = employees[i]; + data.Add(new object[] + { + emp.no, + emp.fullName, + emp.dutyTimeName, + emp.checkInDate, + emp.checkInLocation, + emp.checkInTime, + emp.checkedOutDate, + emp.checkOutLocation, + emp.checkOutTime, + emp.remark + }); + } + + // เขียนข้อมูลลง Excel ครั้งเดียว + 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.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.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Top; + + // Center Align: คอลัมน์ 1 (A), 3 (C), 4 (D), 6 (F), 7 (G), 9 (I) + var centerColumns = new[] { 1, 3, 4, 6, 7, 9 }; + foreach (var col in centerColumns) + { + worksheet.Cells[startRow, col, startRow + totalRows - 1, col].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; + } + + // Left Align: คอลัมน์ 2 (B), 5 (E), 8 (H), 10 (J) + var leftColumns = new[] { 2, 5, 8, 10 }; + foreach (var col in leftColumns) + { + worksheet.Cells[startRow, col, startRow + totalRows - 1, col].Style.HorizontalAlignment = ExcelHorizontalAlignment.Left; + } + } } - - // เขียนข้อมูลลง Excel ครั้งเดียว - worksheet.Cells[startRow, 1].LoadFromArrays(data); - - // กำหนดสไตล์ตัวบาง + ขอบ - using (var range = worksheet.Cells[startRow, 1, startRow + totalRows - 1, colCount]) + else { - range.Style.Font.Bold = false; - 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.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Top; - - // Center Align: คอลัมน์ 1 (A), 3 (C), 4 (D), 6 (F), 7 (G), 9 (I) - var centerColumns = new[] { 1, 3, 4, 6, 7, 9 }; - foreach (var col in centerColumns) - { - worksheet.Cells[startRow, col, startRow + totalRows - 1, col].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; - } - - // Left Align: คอลัมน์ 2 (B), 5 (E), 8 (H), 10 (J) - var leftColumns = new[] { 2, 5, 8, 10 }; - foreach (var col in leftColumns) - { - worksheet.Cells[startRow, col, startRow + totalRows - 1, col].Style.HorizontalAlignment = ExcelHorizontalAlignment.Left; - } + // กรณีไม่มีข้อมูล ใส่ข้อความแจ้ง + worksheet.Cells[startRow, 1].Value = "ไม่มีข้อมูล"; + worksheet.Cells[startRow, 1, startRow, colCount].Merge = true; + worksheet.Cells[startRow, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; + worksheet.Cells[startRow, 1].Style.Font.Bold = true; } // ส่วนสรุปท้ายตาราง