當前位置:首頁 > IT技術(shù) > Web編程 > 正文

.Net Core+NPOI快速導(dǎo)入導(dǎo)出Excel
2021-10-22 09:55:25

Excel導(dǎo)入導(dǎo)出在開發(fā)中是非常常見的,對Excel操作,NPOI使用的是最常用的,但單單用NPOI,要寫得代碼還是比較多的,可以借助一個Npoi.Mapper庫,操作起來就非常簡單了,十來行代碼就可以對Excel和List互相轉(zhuǎn)換,非常簡便。

一、簡單封裝

首先NuGet引入NPOI和Npoi.Mapper

.Net Core+NPOI快速導(dǎo)入導(dǎo)出Excel_用戶名

?

?

?

增加一個ExcelHelper



public class ExcelHelper
{
/// <summary>
/// List轉(zhuǎn)Excel
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">數(shù)據(jù)</param>
/// <param name="sheetName">表名</param>
/// <param name="overwrite">true,覆蓋單元格,false追加內(nèi)容(list和創(chuàng)建的excel或excel模板)</param>
/// <param name="xlsx">true-xlsx,false-xls</param>
/// <returns>返回文件</returns>
public static MemoryStream ParseListToExcel<T>(List<T> list, string sheetName = "sheet1", bool overwrite = true, bool xlsx = true) where T : class
{
var mapper = new Mapper();
MemoryStream ms = new MemoryStream();
mapper.Save<T>(ms, list, sheetName, overwrite, xlsx);
return ms;
}
/// <summary>
/// Excel轉(zhuǎn)為List
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fileStream"></param>
/// <param name="sheetname"></param>
/// <returns></returns>
public static List<T> ParseExcelToList<T>(Stream fileStream, string sheetname = "") where T : class
{
List<T> ModelList = new List<T>();
var mapper = new Mapper(fileStream);
List<RowInfo<T>> DataList = new List<RowInfo<T>>();
if (!string.IsNullOrEmpty(sheetname))
{
DataList = mapper.Take<T>(sheetname).ToList();
}
else
{
DataList = mapper.Take<T>().ToList();
}

if (DataList != null && DataList.Count > 0)
{
foreach (var item in DataList)
{
ModelList.Add(item.Value);
}
}
return ModelList;
}
}


?

增加一個User Model用作導(dǎo)出,Column指明導(dǎo)出時表頭顯示的列名



public class User
{
[Column("用戶ID")]
public int UserId { get; set; }
[Column("用戶名")]
public string UserName { get; set; }
[Column("手機號")]
public string Phone { get; set; }
[Column("郵箱")]
public string Email { get; set; }
}


?

增加一個FileController控制器



public class FileController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Upload()
{
//用戶List
List<User> users = new List<User>();
//獲取到上傳的文件
var file = Request.Form.Files[0];
//文件轉(zhuǎn)成流
using (var fileStream = file.OpenReadStream())
{
//excel轉(zhuǎn)實體
users = ExcelHelper.ParseExcelToList<User>(fileStream, "Sheet1");
}
return Content(JsonConvert.SerializeObject(users));

}
public IActionResult DownLoad()
{
//生成List
List<User> usrs = new List<User>();
for(int i=1;i<=10;i++)
{
User user = new User()
{
UserId = i,
UserName = "用戶" + i,
Phone = "13800000000",
Email = $"user{i}@qq.com"
};
usrs.Add(user);
}
//List轉(zhuǎn)為Excel文件
var fileStream = ExcelHelper.ParseListToExcel(usrs);
return File(fileStream.ToArray(), "application/vnd.ms-excel", "用戶信息.xlsx");
}
}


?

Index的視圖



@{
}
<a href="/File/DownLoad">導(dǎo)出excel文件</a>
<form action="/File/Upload" method="post" enctype="multipart/form-data" >
選擇excel文件: <input type="file" name="file" />
<input type="submit" value="上傳excel" />
</form>


?

運行后界面

?

?.Net Core+NPOI快速導(dǎo)入導(dǎo)出Excel_導(dǎo)出excel_02

二、導(dǎo)出

導(dǎo)出,點擊導(dǎo)出excel文件

.Net Core+NPOI快速導(dǎo)入導(dǎo)出Excel_上傳_03

?

?

?

打開導(dǎo)出的文件,內(nèi)容正是生成的列表信息

?.Net Core+NPOI快速導(dǎo)入導(dǎo)出Excel_導(dǎo)出excel_04

?

?

三、導(dǎo)入

?導(dǎo)入,把我們剛下載的用戶信息.xlsx上傳到后臺

.Net Core+NPOI快速導(dǎo)入導(dǎo)出Excel_導(dǎo)出excel_05

?

可以看到剛才的excel已經(jīng)轉(zhuǎn)換為User的集合了

.Net Core+NPOI快速導(dǎo)入導(dǎo)出Excel_用戶信息_06

?

?

?

?

.Net Core+NPOI快速導(dǎo)入導(dǎo)出Excel_上傳_07

?

?

?

這個組件操作Excel和Model的互相轉(zhuǎn)換非常簡單快捷,推薦使用。

源碼地址:??https://github.com/weixiaolong325/ExcelDemo??

本文摘自 :https://blog.51cto.com/u

開通會員,享受整站包年服務(wù)立即開通 >