RestSharp RestSharp是一個輕量的,不依賴任何第三方的模擬Http的組件或者類庫。RestSharp具體以下特性;支持net4.0++,支持HTTP的GET, POST, PUT, HEAD, OPTIONS, DELETE等操作,支持oAuth 1, oAuth 2, Basic, ...
在Asp.Net Core開發中,使用NPOI將數據導出到Excel文件中,並返回給前端。
service 層代碼:
/// <summary>
/// 將數據導出到excel
/// </summary>
/// <param name="projectId"></param>
/// <param name="ids"></param>
/// <returns></returns>
public async Task<IWorkbook> ExportToExcel(Guid projectId, List<Guid> ids = null)
{
var entities = await attendanceRecordRepository.Find(x =>
x.ProjectId == projectId)
.ToListAsync();
if (entities == null || entities.Count == 0) return null;
//創建工作簿
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("考勤記錄");
//添加表頭
IRow tableHeader = sheet.CreateRow(0);
var colNames = new List<string>()
{
"姓名", "身份證號", "工號", "人員類型", "辦公室", "工種", "電話號碼", "狀態", "打卡時間"
};
for (int i = 0; i < colNames.Count; i++)
{
tableHeader.CreateCell(i).SetCellValue(colNames[i]);
// 自適應寬高
sheet.AutoSizeColumn(i);
}
// 將數據寫入表格中
if (ids == null || ids.Count == 0)
{
// 導出全部
for (int i = 0; i < entities.Count; i++)
{
// 跳過表頭
var row = sheet.CreateRow(i + 1);
row.CreateCell(0).SetCellValue(entities[i].Name);
row.CreateCell(1).SetCellValue(entities[i].ID_card);
row.CreateCell(2).SetCellValue(entities[i].EmployeeNumber);
row.CreateCell(3).SetCellValue(entities[i].PersonnelType);
row.CreateCell(4).SetCellValue(entities[i].OfficeLocation);
row.CreateCell(5).SetCellValue(entities[i].PostName);
row.CreateCell(6).SetCellValue(entities[i].PhoneNumber);
row.CreateCell(7).SetCellValue(entities[i].Type);
row.CreateCell(8).SetCellValue(entities[i].CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
else
{
// 導出部分
int rowIndex = 1;
foreach (var entity in entities)
{
foreach (var id in ids)
{
if (entity.Id == id)
{
var row = sheet.CreateRow(rowIndex);
row.CreateCell(0).SetCellValue(entity.Name);
row.CreateCell(1).SetCellValue(entity.ID_card);
row.CreateCell(2).SetCellValue(entity.EmployeeNumber);
row.CreateCell(3).SetCellValue(entity.PersonnelType);
row.CreateCell(4).SetCellValue(entity.OfficeLocation);
row.CreateCell(5).SetCellValue(entity.PostName);
row.CreateCell(6).SetCellValue(entity.PhoneNumber);
row.CreateCell(7).SetCellValue(entity.Type);
row.CreateCell(8).SetCellValue(entity.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
rowIndex++;
}
}
}
}
return workbook;
}
controller 層代碼:
/// <summary>
/// 將數據導出為Excel文件
/// </summary>
/// <param name="projectId"></param>
/// <param name="ids"></param>
/// <returns></returns>
[HttpPost("export-to-excel")]
public async Task<IActionResult> ExportToExcel(Guid projectId, List<Guid> ids = null)
{
var workbook = await _attendanceRecordService.ExportToExcel(projectId, ids);
if(workbook != null)
{
var path = Path.Combine(webHostEnvironment.ContentRootPath, "FileName");
if (!Directory.Exists(path)) //沒有此路徑就新建
{
Directory.CreateDirectory(path);
}
var fileFullName = Path.Combine(path, $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}.xlsx");
// 將表格寫入文件流
FileStream creatStream = new FileStream(fileFullName, FileMode.Create, FileAccess.Write);
workbook.Write(creatStream);
creatStream.Close();
// 將表格文件轉換成可讀的文件流
FileStream fileStream = new FileStream(fileFullName, FileMode.Open, FileAccess.Read, FileShare.Read); //讀
// 將可讀文件流寫入 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
// 把 byte[] 轉換成 Stream (創建其支持存儲區為記憶體的流。)
MemoryStream stream = new(bytes);
try
{
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
$"{DateTime.Now.ToString("yyyyMMddHHmmss")}考勤記錄");
}
finally
{
System.IO.File.Delete(fileFullName);
}
}
return BadRequest();
}