Excel convert to .NET Object
PM> Install-Package Chsword.Excel2Object
- v1.0.0.36
add ExcelToObject(bytes)
Model
public class ReportModel
{
[Excel("标题",Order=1)]
public string Title { get; set; }
[Excel("用户",Order=2)]
public string Name { get; set; }
}
Model List
var models = new List<ReportModel>
{
new ReportModel{Name="a",Title="b"},
new ReportModel{Name="c",Title="d"},
new ReportModel{Name="f",Title="e"}
};
Convert Object to Excel file.
var exporter = new ExcelExporter();
var bytes = exporter.ObjectToExcelBytes(models);
File.WriteAllBytes("C:\\demo.xls", bytes);
Convert Excel file to Object
var importer = new ExcelImporter();
IEnumerable<ReportModel> result = importer.ExcelToObject<ReportModel>("c:\\demo.xls");
// also can use bytes
//IEnumerable<ReportModel> result = importer.ExcelToObject<ReportModel>(bytes);
With ASP.NET MVC In ASP.NET MVC Model, DisplayAttribute can be supported like ExcelAttribute.