ExcelToObject,Make operating excel as simple as operating objects.
dotnet add package ExcelToObject.Npoi --version 1.0.0- The Web program adds the following code to the Program or Startup
service.AddExcelToObjectNpoiService();- Console Application
IExcelFactory _excelFactory=new NpoiExcelFactory();
- Read a sheet in excel as a list
using (var excelAppService = _excelFactory.Create(System.IO.File.Open("./files/Test.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
var list = excelAppService.ReadListByNameManager<TestImportInput>("Sheet1");
foreach (var item in list)
{
Console.WriteLine($"编码:【{item.Code}】,名称:【{item.Name}】,备注:【{item.Remark}】");
}
}- Read a sheet in Excel as a single object
using (var excelAppService = _excelFactory.Create(System.IO.File.Open("./files/Test.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
var user = excelAppService.ReadByNameManager<TestUserOutput>("Sheet2");
Console.WriteLine($"姓名:{user.TrueName},年龄:{user.Age},性别:{user.Gender}");
}- Write a list object to the Excel Sheet page
using (var excelAppService = _excelFactory.Create(System.IO.File.Open("./files/Test.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
excelAppService.WriteListByNameManager(new List<TestImportInput> { new TestImportInput { Code = "", Name = "", Remark = "" } }, "Sheet2");
excelAppService.Write("./files/Test_copy1.xlsx");
}- Write an object to the excel sheet page
using (var excelAppService = _excelFactory.Create(System.IO.File.Open("./files/Test.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
excelAppService.WriteByNameManager(new TestUserOutput { Age=18, Gender="男", TrueName="赵六" }, "Sheet2");
excelAppService.Write("./files/Test_copy1.xlsx");
}- Match with the attribute name of the entity through the name manager of Excel
- Get entity attributes through the passed in entity. Find out the name manager in sheetName and the coordinates of each name. Read.
- Fork the repository
- Create Feat_xxx branch
- Commit your code
- Create Pull Request
