Excel導(dǎo)入導(dǎo)出在開發(fā)中是非常常見的,對(duì)Excel操作,NPOI使用的是最常用的,但單單用NPOI,要寫得代碼還是比較多的,可以借助一個(gè)Npoi.Mapper庫(kù),操作起來就非常簡(jiǎn)單了,十來行代碼就可以對(duì)Excel和List互相轉(zhuǎn)換,非常簡(jiǎn)便。
一、簡(jiǎn)單封裝首先NuGet引入NPOI和Npoi.Mapper
?
?
?
增加一個(gè)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)為L(zhǎng)ist
/// </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;
}
}
?
增加一個(gè)User Model用作導(dǎo)出,Column指明導(dǎo)出時(shí)表頭顯示的列名
public class User
{
[Column("用戶ID")]
public int UserId { get; set; }
[Column("用戶名")]
public string UserName { get; set; }
[Column("手機(jī)號(hào)")]
public string Phone { get; set; }
[Column("郵箱")]
public string Email { get; set; }
}
?
增加一個(gè)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)實(shí)體
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>
?
運(yùn)行后界面
?
?
二、導(dǎo)出導(dǎo)出,點(diǎn)擊導(dǎo)出excel文件
?
?
?
打開導(dǎo)出的文件,內(nèi)容正是生成的列表信息
?
?
?
三、導(dǎo)入?導(dǎo)入,把我們剛下載的用戶信息.xlsx上傳到后臺(tái)
?
可以看到剛才的excel已經(jīng)轉(zhuǎn)換為User的集合了
?
?
?
?
?
?
?
這個(gè)組件操作Excel和Model的互相轉(zhuǎn)換非常簡(jiǎn)單快捷,推薦使用。
本文摘自 :https://blog.51cto.com/u