网上C#导出Excel的方法有很多。但用来用去感觉不够自动化。于是花了点时间,利用特性做了个比较通用的导出方法。只需要根据实体类,自动导出想要的数据
1.在NuGet上安装Aspose.Cells或者用微软自带类库也可以
2.需要导出的数据的实例类:
using System.ComponentModel; using System.Reflection; using System.Runtime.Serialization; public class OrderReport { [DisplayName("订单编号")] public string orderNo { get; set; } [IgnoreDataMember] public DateTime orderTime { get; set; } [DisplayName("订单时间")] public String orderTime_fomart { get { return orderTime.ToShortDateString(); } } [DisplayName("商品编码")] public string itemCode { get; set; } [DisplayName("商品名称")] public string itemName { get; set; } }