diff --git a/WebApi_data_value/Controllers/ParkingLogsController.cs b/WebApi_data_value/Controllers/ParkingLogsController.cs index 8f8fff4..581e806 100644 --- a/WebApi_data_value/Controllers/ParkingLogsController.cs +++ b/WebApi_data_value/Controllers/ParkingLogsController.cs @@ -118,24 +118,25 @@ namespace WebApi_data_value.Controllers } } + // 下載 Excel 檔案 [HttpGet("DownloadExcel")] public async Task DownloadExcel(DateTime startDate, DateTime endDate, string interval) { - // 確保日期有效 + if (startDate == default || endDate == default) { return BadRequest(new { message = "日期無效。" }); } - // 確保開始時間早於結束時間 + if (startDate >= endDate) { return BadRequest(new { message = "結束日期必須晚於開始日期。" }); } - // 調用 GetHourlyParkingLogs 方法以獲取停車紀錄 - var parkingLogsResult = await GetHourlyParkingLogs(startDate, endDate, interval); + // 調用 GetHourlyParkingLogs 方法以獲取所有停車紀錄 + var parkingLogsResult = await GetHourlyParkingLogs(startDate, endDate, interval, 1, int.MaxValue); if (parkingLogsResult is NotFoundResult) { @@ -156,6 +157,7 @@ namespace WebApi_data_value.Controllers worksheet.Cell(1, 6).Value = "臨停車位"; worksheet.Cell(1, 7).Value = "下載日期"; + for (int i = 0; i < records.Count; i++) { var log = records[i]; @@ -165,11 +167,10 @@ namespace WebApi_data_value.Controllers worksheet.Cell(i + 2, 4).Value = log.RemainingSpaces; worksheet.Cell(i + 2, 5).Value = log.MonthlyRentSpaces; worksheet.Cell(i + 2, 6).Value = log.TemporaryRentSpaces; - worksheet.Cell(2, 7).Value = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss dddd"); - + worksheet.Cell(i + 2, 7).Value = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss dddd"); } - + worksheet.Column(1).Width = 25; worksheet.Column(2).Width = 10; worksheet.Column(3).Width = 15; @@ -183,7 +184,6 @@ namespace WebApi_data_value.Controllers workbook.SaveAs(stream); var content = stream.ToArray(); - string currentDate = DateTime.Now.ToString("yyyy-MM-dd"); string weekDay = DateTime.Now.ToString("dddd", new CultureInfo("zh-TW")); @@ -193,8 +193,6 @@ namespace WebApi_data_value.Controllers } } } - - // 獲取停車紀錄 private async Task GetParkingLogs(DateTime startDate, DateTime endDate, int pageNumber, int pageSize) { @@ -282,8 +280,6 @@ namespace WebApi_data_value.Controllers // 將 interval 轉換為整數 int hourInterval = int.Parse(interval); var formattedLogs = new List(); - - // 從 startDate 開始,每次加 hourInterval 小時,直到 endDate for (DateTime currentTime = startDate; currentTime <= endDate; currentTime = currentTime.AddHours(hourInterval)) { // 查找該時間的停車紀錄