From 04daae2fc27f75b0d8558a506185bf19f6ba97d4 Mon Sep 17 00:00:00 2001 From: qi_0527 Date: Mon, 18 Nov 2024 16:39:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=87=BA=E5=8E=BB=E5=8A=A0?= =?UTF-8?q?=E6=94=B9Bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ParkingLogsController.cs | 5 +- .../Yuntech_in_car_tableController.cs | 94 +++++++++++++------ 2 files changed, 69 insertions(+), 30 deletions(-) diff --git a/WebApi_data_value/Controllers/ParkingLogsController.cs b/WebApi_data_value/Controllers/ParkingLogsController.cs index 5e74035..80b534d 100644 --- a/WebApi_data_value/Controllers/ParkingLogsController.cs +++ b/WebApi_data_value/Controllers/ParkingLogsController.cs @@ -167,10 +167,9 @@ 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(i + 2, 7).Value = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss dddd"); + } - - + worksheet.Cell( 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; diff --git a/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs b/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs index 25de7b0..771048f 100644 --- a/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs +++ b/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs @@ -10,6 +10,7 @@ using Parking_space_WebAPI.Services; using Parking_space_WebAPI.ViewModel; using Parking_space_WebAPI.Authorization; using OfficeOpenXml; +using System.Web; namespace Parking_space_WebAPI.Controllers @@ -52,7 +53,6 @@ namespace Parking_space_WebAPI.Controllers } #endregion - #region 獲取進入雲科的100筆資料 /// /// 獲取進入雲科的100筆資料 @@ -69,6 +69,7 @@ namespace Parking_space_WebAPI.Controllers in_time = c.in_time, out_time = c.out_time, location = c.location, + out_location = c.out_location, } ).Skip(num).Take(100).ToListAsync(); @@ -96,6 +97,7 @@ namespace Parking_space_WebAPI.Controllers in_time = c.in_time, out_time = c.out_time, location = c.location, + out_location = c.out_location, }).Skip(0).Take(500).ToListAsync(); @@ -127,6 +129,7 @@ namespace Parking_space_WebAPI.Controllers in_time = c.in_time, out_time = c.out_time, location = c.location, + out_location = c.out_location, }).ToListAsync(); @@ -153,6 +156,7 @@ namespace Parking_space_WebAPI.Controllers in_time = c.in_time, out_time = c.out_time, location = c.location, + out_location = c.out_location, }).ToListAsync(); @@ -226,7 +230,7 @@ namespace Parking_space_WebAPI.Controllers } #endregion - #region 尋找所有進出校園的地點 + #region 尋找所有進入校園的地點 /// ///尋找所有進出校園的地點 /// @@ -235,18 +239,37 @@ namespace Parking_space_WebAPI.Controllers public async Task> Get_in_Yuntech_location_name() { var location_name_table = await _context.yuntech_in_car_table - .GroupBy(c=>c.location) + .GroupBy(c => new { c.location }) .Select(group => new { - location_name = group.Key, + location_name = group.Key.location, // 取得進入地點 + }) .ToListAsync(); - return location_name_table; } #endregion - + + #region 尋找所有離開校園的地點 + /// + ///尋找所有離開校園的地點 + /// + /// + [HttpGet("out_location_name")] + public async Task> Get_out_Yuntech_location_name() + { + var out_location_name_table = await _context.yuntech_in_car_table + .GroupBy(c => new { c.out_location }) + .Select(group => new + { + out_location_name = group.Key.out_location // 取得離開地點 + }) + .ToListAsync(); + + return out_location_name_table; + } + #endregion #region 新增資料 /// @@ -363,7 +386,6 @@ namespace Parking_space_WebAPI.Controllers } #endregion - #region City_parking 新增進入資料 /// /// City_parking 新增進入資料 @@ -479,7 +501,6 @@ namespace Parking_space_WebAPI.Controllers } #endregion - #region 新增出去資料 /// /// 新增出去雲科的車輛資料 @@ -680,7 +701,6 @@ namespace Parking_space_WebAPI.Controllers } #endregion - #region 刪除資料 /// /// 刪除指定資料 @@ -720,13 +740,14 @@ namespace Parking_space_WebAPI.Controllers where c.location == id where c.in_time >= start_time where c.in_time <= end_time - orderby c.in_time descending // 按進入時間降序排列 + orderby c.in_time descending select new Yuntech_in_car_table { license_plate_number = c.license_plate_number, in_time = c.in_time, out_time = c.out_time, location = c.location, + out_location = c.out_location, }).ToListAsync(); using (ExcelPackage package = new ExcelPackage()) @@ -737,13 +758,18 @@ namespace Parking_space_WebAPI.Controllers worksheet.Cells["A1"].Value = "進入位置"; worksheet.Cells["B1"].Value = "車牌號碼"; worksheet.Cells["C1"].Value = "進入時間"; - worksheet.Cells["D1"].Value = "出去時間"; + worksheet.Cells["D1"].Value = "出去區域"; + worksheet.Cells["E1"].Value = "出去時間"; + worksheet.Cells["F1"].Value = "下載日期"; // 設置列寬 - worksheet.Column(1).Width = 20; - worksheet.Column(2).Width = 15; - worksheet.Column(3).Width = 25; - worksheet.Column(4).Width = 25; + worksheet.Column(1).Width = 20; + worksheet.Column(2).Width = 15; + worksheet.Column(3).Width = 25; + worksheet.Column(4).Width = 20; + worksheet.Column(5).Width = 25; + worksheet.Column(6).Width = 25; + worksheet.Cells["F2"].Value = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss dddd"); // 添加資料 int row = 2; @@ -751,9 +777,9 @@ namespace Parking_space_WebAPI.Controllers { worksheet.Cells["A" + row].Value = item.location; worksheet.Cells["B" + row].Value = item.license_plate_number; - worksheet.Cells["C" + row].Value = item.in_time?.ToString("yyyy-MM-dd HH:mm:ss") ?? ""; - worksheet.Cells["D" + row].Value = item.out_time?.ToString("yyyy-MM-dd HH:mm:ss") ?? ""; - + worksheet.Cells["C" + row].Value = item.in_time?.ToString("yyyy-MM-dd HH:mm:ss") ?? ""; + worksheet.Cells["D" + row].Value = item.out_location; + worksheet.Cells["E" + row].Value = item.out_time?.ToString("yyyy-MM-dd HH:mm:ss") ?? ""; row++; } @@ -761,8 +787,13 @@ namespace Parking_space_WebAPI.Controllers // 將ExcelPackage保存到內存流中 MemoryStream stream = new MemoryStream(package.GetAsByteArray()); + // 處理文件名中的特殊字符(例如冒號、空格) + string fileName = $"{id}_car_table_{start_time:yyyyMMddHHmmss}_{end_time:yyyyMMddHHmmss}.xlsx"; + string encodedFileName = HttpUtility.UrlEncode(fileName) + .Replace("%20", "_") + .Replace("%3A", "-"); // 設置HttpResponseMessage的內容 - Response.Headers.Add("Content-Disposition", $"attachment; filename={id}_car_table_{start_time:yyyyMMddHHmmss}_{end_time:yyyyMMddHHmmss}.xlsx"); + Response.Headers.Add("Content-Disposition", $"attachment; filename={encodedFileName}"); return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); } } @@ -793,6 +824,7 @@ namespace Parking_space_WebAPI.Controllers in_time = c.in_time, out_time = c.out_time, location = c.location, + out_location = c.out_location, }).ToListAsync(); // 創建ExcelPackage對象 @@ -804,12 +836,15 @@ namespace Parking_space_WebAPI.Controllers worksheet.Cells["A1"].Value = "進入位置"; worksheet.Cells["B1"].Value = "車牌號碼"; worksheet.Cells["C1"].Value = "進入時間"; - worksheet.Cells["D1"].Value = "出去時間"; + worksheet.Cells["D1"].Value = "出去區域"; + worksheet.Cells["E1"].Value = "出去時間"; + // 設置列寬 worksheet.Column(1).Width = 20; worksheet.Column(2).Width = 15; worksheet.Column(3).Width = 25; - worksheet.Column(4).Width = 25; + worksheet.Column(4).Width = 20; + worksheet.Column(5).Width = 25; // 添加資料 int row = 2; foreach (var item in in_car_table) @@ -817,7 +852,8 @@ namespace Parking_space_WebAPI.Controllers worksheet.Cells["A" + row].Value = item.location; worksheet.Cells["B" + row].Value = item.license_plate_number; worksheet.Cells["C" + row].Value = item.in_time?.ToString("yyyy-MM-dd HH:mm:ss") ?? ""; - worksheet.Cells["D" + row].Value = item.out_time?.ToString("yyyy-MM-dd HH:mm:ss") ?? ""; + worksheet.Cells["D" + row].Value = item.out_location; + worksheet.Cells["E" + row].Value = item.out_time?.ToString("yyyy-MM-dd HH:mm:ss") ?? ""; row++; } @@ -831,7 +867,6 @@ namespace Parking_space_WebAPI.Controllers } #endregion - #region 生成全部進入車輛EXCEL /// /// 生成全部進入車輛EXCEL @@ -848,8 +883,9 @@ namespace Parking_space_WebAPI.Controllers in_time = c.in_time, out_time = c.out_time, location = c.location, + out_location = c.out_location, - }).ToListAsync(); + }).ToListAsync(); // 創建ExcelPackage對象 using (ExcelPackage package = new ExcelPackage()) @@ -860,12 +896,15 @@ namespace Parking_space_WebAPI.Controllers worksheet.Cells["A1"].Value = "進入位置"; worksheet.Cells["B1"].Value = "車牌號碼"; worksheet.Cells["C1"].Value = "進入時間"; - worksheet.Cells["D1"].Value = "出去時間"; + worksheet.Cells["D1"].Value = "出去區域"; + worksheet.Cells["E1"].Value = "出去時間"; + // 設置列寬 worksheet.Column(1).Width = 20; worksheet.Column(2).Width = 15; worksheet.Column(3).Width = 25; - worksheet.Column(4).Width = 25; + worksheet.Column(4).Width = 20; + worksheet.Column(5).Width = 25; // 添加資料 int row = 2; @@ -874,7 +913,8 @@ namespace Parking_space_WebAPI.Controllers worksheet.Cells["A" + row].Value = item.location; worksheet.Cells["B" + row].Value = item.license_plate_number; worksheet.Cells["C" + row].Value = item.in_time?.ToString("yyyy-MM-dd HH:mm:ss") ?? ""; - worksheet.Cells["D" + row].Value = item.out_time?.ToString("yyyy-MM-dd HH:mm:ss") ?? ""; + worksheet.Cells["D" + row].Value = item.out_location; + worksheet.Cells["E" + row].Value = item.out_time?.ToString("yyyy-MM-dd HH:mm:ss") ?? ""; row++; }