ASP .Net Core使用EPPlus實現Api導入導出,這裏使用是EPPlus 4.5.2.1版本,.Net Core 2.2。在linux上運行的時候須要安裝libgdiplus 。linux
下面咱們看下如何實現導出導入的功能。api
新建項目ASP.NET Core Web Api 、添加Test類服務器
public class Test { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; }
}
導出Excel文件app
新建控制器ExecelControllerasync
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using OfficeOpenXml; namespace WebApplication5.Controllers { [ApiController] public class ExecelController : ControllerBase { [Route("api/Execel/TOExecel")] public IActionResult TOExecel() { var list = new List<Test>(); list.Add(new Test() { Id = 1, Name = "Test", Age = 22, }); byte[] fileContents; using (ExcelPackage package = new ExcelPackage()) { ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(".Net Core 導出"); worksheet.Cells[1, 1].Value = "序號"; worksheet.Cells[1, 2].Value = "Id"; worksheet.Cells[1, 3].Value = "名稱"; worksheet.Cells[1, 4].Value = "年齡"; int i = 2; foreach (var item in list) { worksheet.Cells["A" + i].Value = i - 1; worksheet.Cells["B" + i].Value = item.Id; worksheet.Cells["C" + i].Value = item.Name; worksheet.Cells["D" + i].Value = item.Age; i = i + 1; } fileContents = package.GetAsByteArray(); if (fileContents == null || fileContents.Length == 0) { return NotFound(); } } return File(fileContents, "application/ms-excel", $"{Guid.NewGuid().ToString()}.xlsx"); } } }
導入Excel文件測試
[Route("api/Execel/Import")] public async Task<IActionResult> Import(IFormFile excelFile) { var msg = ""; if (excelFile == null || excelFile.Length <= 0) { msg = "請選擇導入文件!"; return Ok(msg); } if (!Path.GetExtension(excelFile.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase)) { msg = "請選擇導入文件爲.xlsx的後綴名!"; return Ok(msg); } try { using (var stream = new MemoryStream()) { await excelFile.CopyToAsync(stream); using (var package = new ExcelPackage(stream)) { StringBuilder sb = new StringBuilder(); ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; int rowCount = worksheet.Dimension.Rows; int ColCount = worksheet.Dimension.Columns; bool bHeaderRow = true; for (int row = 2; row <= rowCount; row++) { Test model = new Test(); for (int col = 1; col <= ColCount; col++) { if (bHeaderRow) { switch (col) { case 1: model.Id = int.Parse(worksheet.Cells[row, col].Value.ToString()); break; case 2: model.Name = worksheet.Cells[row, col].Value.ToString(); break; case 3: model.Age = int.Parse(worksheet.Cells[row, col].Value.ToString()); break; } } else { switch (col) { case 1: model.Id = int.Parse(worksheet.Cells[row, col].Value.ToString()); break; case 2: model.Name = worksheet.Cells[row, col].Value.ToString(); break; case 3: model.Age = int.Parse(worksheet.Cells[row, col].Value.ToString()); break; } } } //插入model便可 } } } msg = "導入成功!"; return Ok(msg); } catch (Exception ex) { msg = ex.Message; return Ok(msg); } }
按照格式導入便可完成導入功能。ui
這裏導出是經過流的方式進行導出的,通常來講也能夠先把Excel保存到服務器,而後直接經過Url訪問服務器的文件的地址就能夠實如今線下載。經過流導出的話就不須要去訪問服務器文件了,訪問接口查詢數據就直接導出了。spa
在導入的時候目前測試僅僅支持.xlsx格式的,.xls格式的Excel在基於流建立ExcelPackage類的新實例的時候會報爲空的錯誤。var package = new ExcelPackage(stream)還有待修改。excel