error excel leave
Some checks failed
release-dev / release-dev (push) Failing after 12s

This commit is contained in:
kittapath 2025-10-16 23:47:27 +07:00
parent 85e1c95f97
commit f8a71f6624

View file

@ -2133,52 +2133,64 @@ namespace BMA.EHR.Leave.Service.Controllers
int colCount = 10;
int totalRows = employees.Count;
// เตรียม List<object[]> สำหรับใช้กับ LoadFromArrays
var data = new List<object[]>(totalRows);
for (int i = 0; i < totalRows; i++)
// ตรวจสอบว่ามีข้อมูลหรือไม่
if (totalRows > 0)
{
var emp = employees[i];
data.Add(new object[]
// เตรียม List<object[]> สำหรับใช้กับ LoadFromArrays
var data = new List<object[]>(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;
}
// ส่วนสรุปท้ายตาราง