更新自動上傳excel檔的後端功能

This commit is contained in:
qi_0527 2024-09-12 14:12:39 +08:00
parent 0007090004
commit 82530baf77
3 changed files with 86 additions and 1 deletions

View File

@ -752,7 +752,63 @@ namespace Parking_space_WebAPI.Controllers
}
#endregion
#region Excel文件
/// <summary>
/// 自動生成包含時間範圍內車輛資料的 Excel 文件
/// </summary>
/// <param name="id">位置名稱</param>
/// <param name="start_time">起始時間</param>
/// <param name="end_time">結束時間</param>
/// <returns></returns>
[HttpGet("auto_export_excel")]
public async Task<IActionResult> ExportYuntech_in_car_table_date_to_excel_auto()
{
var start_time = DateTime.Now.AddHours(-12);
var end_time = DateTime.Now;
var in_car_table = await (from c in _context.yuntech_in_car_table
where c.in_time >= start_time
where c.in_time <= end_time
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,
}).ToListAsync();
// 創建ExcelPackage對象
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("YuntechInCarTable");
// 添加標題行
worksheet.Cells["A1"].Value = "進入位置";
worksheet.Cells["B1"].Value = "車牌號碼";
worksheet.Cells["C1"].Value = "進入時間";
worksheet.Cells["D1"].Value = "出去時間";
// 添加資料
int row = 2;
foreach (var item in in_car_table)
{
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(); // 可以自行調整日期時間的格式
worksheet.Cells["D" + row].Value = item.out_time.ToString();
row++;
}
// 將ExcelPackage保存到內存流中
MemoryStream stream = new MemoryStream(package.GetAsByteArray());
// 設置HttpResponseMessage的內容
Response.Headers.Add("Content-Disposition", "attachment; filename=test.xlsx");
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}
#endregion
#region EXCEL

View File

@ -10,6 +10,7 @@ using WebApi_data_value.Models;
using Parking_space_WebAPI.Authorization;
using OfficeOpenXml;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using Parking_space_WebAPI.Models;
namespace WebApi_data_value.Controllers
{
@ -189,7 +190,8 @@ namespace WebApi_data_value.Controllers
// 確認EXCEL格式符合規格
string user_name_check = worksheet.Cells[1, 3].Text;
string user_license_plate_number_check = worksheet.Cells[1, 5].Text;
if (user_name_check != "車主姓名" && user_license_plate_number_check != "車牌號碼")
string user_phone_number_check = worksheet.Cells[1, 4].Text;
if (user_name_check != "車主姓名" && user_license_plate_number_check != "車牌號碼" && user_phone_number_check !="聯絡電話")
{
return BadRequest("無效的Excel文件。");
}
@ -233,6 +235,7 @@ namespace WebApi_data_value.Controllers
{
string user_name = worksheet.Cells[row, 3].Text;
string user_license_plate_number = worksheet.Cells[row, 5].Text;
string user_phone_number = worksheet.Cells[row, 4].Text;
string user_state_check = "True";
try
{
@ -241,6 +244,7 @@ namespace WebApi_data_value.Controllers
var now_data = new Yuntech_parking_user_list();
now_data.user_name = user_name;
now_data.user_license_plate_number = user_license_plate_number;
now_data.user_phone_number = user_phone_number;
now_data.user_state_check = "True";
_context.yuntech_parking_user_list.Add(now_data);
}
@ -273,7 +277,30 @@ namespace WebApi_data_value.Controllers
}
#endregion
#region
/// <summary>
/// 透過車牌號碼搜尋是否為月租名單成員
/// </summary>
/// <param name="id">車牌號碼 ex:ABC4321</param>
/// <returns></returns>
[HttpGet("license_plate_number-{id}")]
public async Task<IEnumerable<Yuntech_parking_user_list>> Getlicense_plate_number(string id)
{
var parking_user_list = await (from c in _context.yuntech_parking_user_list
where c.user_license_plate_number == id
select new Yuntech_parking_user_list
{
user_name = c.user_name,
user_license_plate_number = c.user_license_plate_number,
user_state_check = c.user_state_check,
user_phone_number = c.user_phone_number,
}).ToListAsync();
return parking_user_list;
}
#endregion

View File

@ -6,5 +6,7 @@
public string? user_license_plate_number { get; set; }=null;
public string? user_state_check { get; set; } = null;
public string? user_phone_number { get; set; } = null;
}
}