BS导出csv文件的通用方法(.net)

时间:2021-04-30 06:32:57

最近把以前项目里用的导出文件的功能提取成了dll,通过读取Attribute来得到要导出的表头(没有支持多语言),使用时只要组织好要导出的数据,调用方法就好了,希望对大家有用。

使用时只需引用下载包里bin里面的ExportData.dll文件即可,demo以下:

    public class HomeController : Controller
{
public ActionResult Index()
{
List<Model> data = new List<Model>(); data.Add(new Model { ID = 1, Name = "名字1" });
data.Add(new Model { ID = 2, Name = "小明" });
data.Add(new Model { ID = 3, Name = "小红" }); ExportFile.ExportToCSV<Model>(data, "test"); return View();
}
} public class Model
{
[ExportHeader("序号", 0)]
public int ID { get; set; }
[ExportHeader("名称", 1)]
public string Name { get; set; }
}

为了简单我把Model类与controller放在一起了,大家使用时请把它单独提出去。

效果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAANAAAACJCAIAAADSRyEcAAAJ/klEQVR4nO2d7U8byR3H+Wcqv+m7SvvSkv+Qqi/6opXaqi9OatWL1KtSna65HCGQrk46ivMAToBcC4TEMRgIJkfJJQFMDAEChAeDY2weF/MQ7qLti033zO7s7Ox4Zna8+/voK2TMru2f55PZmd25vYa2trbLly//7Oe/ODzTU0sfzKzuffjl7z/97Vc9OhBcTs7O//LNQG51p6ydW7J3xCUNIFyYuZ2avDecK+yd2VPc55KAC9dwEfvzPn4238lvH/w1PrTy7mi9dGJkrVh5Nv2mf2Ds277hdObl6jstXz5hmyALZ/HJ/NVintDPJBM9Y3N3BmaWChUjb7aOJl7mUumn/3kw2NHZ13qr+2aid3Fjf+XdMcMEVji7SUjhkFuGhCuJzODkxtzG0cesFCezs2PjL6qd6x+cWNg8YpgQCef0p+AJ14DCvtml1oGnc9vZ1UMjS+vF+cUVi3Md95Ov1jSGCbtwQR3Judqm6/qfvn6cyZWeLx0YWSvsrucLFue6e9Mvlw8YJozC6RcnDYEUTieYGH1+Z6RvYm18fs/I/MZ+qWx1LjU2M7GwxzAhFY5uy7oDX9rt1GRrcvZJbtfI6OzuVlmrdi7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