1.NuGet 添加Npoi.Mapper 引用
2.初始化
private void button1_Click(object sender, EventArgs e) { //数据导出测试 List<Person> persons = new List<Person>(); persons.Add(new Person { Id = 1, Name = "fan1", Age = 11, BirthDate = DateTime.Now.AddYears(11), IsActive = true }); persons.Add(new Person { Id = 1, Name = "fan2", Age = 12, BirthDate = DateTime.Now.AddYears(12), IsActive = true }); persons.Add(new Person { Id = 1, Name = "fan3", Age = 13, BirthDate = DateTime.Now.AddYears(13), IsActive = true }); persons.Add(new Person { Id = 1, Name = "fan4", Age = 14, BirthDate = DateTime.Now.AddYears(14), IsActive = false }); persons.Add(new Person { Id = 1, Name = "fan4", Age = 14, BirthDate = DateTime.Now.AddYears(14), IsActive = false }); persons.Add(new Person { Id = 1, Name = "fan4", Age = 14, BirthDate = DateTime.Now.AddYears(14), IsActive = false }); persons.Add(new Person { Id = 1, Name = "fan4", Age = 14, BirthDate = DateTime.Now.AddYears(14), IsActive = false }); var mapper = new Npoi.Mapper.Mapper(); mapper.Save("Persons.xlsx", persons, "sheet1", true, true); }
public class Person
{
[Column("编号")]//header的名称
public int Id { get; set; }
[Column("姓名")]
public string Name { get; set; }
[Column("年龄")]
public int Age { get; set; }
[Column("生日", CustomFormat = "yyyy-MM-dd")]
public DateTime BirthDate { get; set; }
[Column("状态")]
public bool IsActive { get; set; }
[Ignore]//忽略此行
public int Ignore { get; set; }
}
标签:Mapper,Name,core,Age,Excel,BirthDate,DateTime,persons,new From: https://www.cnblogs.com/request/p/16597696.html