ASP.NET MVC 将IList<T>导出Excel文档的泛型类(继承自Action

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 21:52 点击:

 

最近MVC项目中要使用到Excel导出功能,在网上找了些资料,自己写了一个通用的泛型类(ExcelResult)。因为是直接继承自ActionResult这个抽象类的,所以用起来很方便,在控制器的Action中直接实例化返回即可。本人的代码功底不是很好,写的代码有点烂,希望大伙指正。

 

废话少说,直接上类完整代码:

  View Code

  1 /// <summary>

  2     /// 提供将泛型集合数据导出Excel文档。

  3     /// </summary>

  4     /// <typeparam name="T"></typeparam>

  5     public class ExcelResult<T> : ActionResult where T : new()

  6     {

  7         public ExcelResult(IList<T> entity, string fileName)

  8         {

  9             this.Entity = entity;

 10             this.FileName = fileName;

 11         }

 12

 13         public ExcelResult(IList<T> entity)

 14         {

 15             this.Entity = entity;

 16

 17             DateTime time = DateTime.Now;

 18             this.FileName = string.Format("{0}_{1}_{2}_{3}",

 19                 time.Month, time.Day, time.Hour, time.Minute);

 20         }

 21

 22         public IList<T> Entity

 23         {

 24             get;

 25             set;

 26         }

 27

 28         public string FileName

 29         {

 30             get;

 31             set;

 32         }

 33

 34         public override void ExecuteResult(ControllerContext context)

 35         {

 36             if (Entity == null)

 37             {

 38                 new EmptyResult().ExecuteResult(context);

 39                 return;

 40             }

 41

 42             SetResponse(context);

 43         }

 44

 45         /// <summary>

 46    &n

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • 浅析.NET下XML数据访问新机制
      • asp.net 面试+笔试题目第1/2页
      • C# 邮件地址是否合法的验证
      • asp.net 设置GridView的选中行的实现代码
      • C#高级编程:数据库连接[1]
      • 经典C++程序1
      • IIS 自动回收导致后台定时器失效的问题解决
      • ASP.NET&#160;GridView列表代码示例
      • Asp.net MVC源码分析--Action Filter的链式调用
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1