ASP.NET MVC 将IList<T>导出Excel文档的泛型类(继承自ActionResult)
最近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 /// 设置并向客户端发送请求响应。
47 /// </summary>
48 /// <param name="context"></param>
49 private void SetResponse(ControllerContext context)
50 {
51 StringBuilder sBuilder = ConvertEntity();
52 byte[] bytestr = Encoding.Unicode.GetBytes(sBuilder.ToString());
53
54 context.HttpContext.Response.Clear();
55 context.HttpContext.Response.ClearContent();
56 context.HttpContext.Response.Buffer = true;
57 context.HttpContext.Response.Charset = "GB2312";
58 context.HttpContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
59 context.HttpContext.Response.ContentType = "application/ms-excel";
60 context.HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls");
61 context.HttpContext.Response.AddHeader("Content-Length", bytestr.Length.ToString());
62 context.HttpContext.Response.Write(sBuilder);
63 context.HttpContext.Response.End();
64 }
65
66 /// <summary>
67 /// 把泛型集合转换成组合Excel表格的字符串。
68 /// </summary>
69 /// <returns></returns>
70 private StringBuilder ConvertEntity()
71 {
72 StringBuilder sb = new StringBuilder();
73
74 AddTableHead(sb);
75 AddTableBody(sb);
76
77 return sb;
78 }
79
80 /// <summary>
81 /// 根据IList泛型集合中的每项的属性值来组合Excel表格。
82 /// </summary>
83 /// <param name=&
补充:Web开发 , ASP.Net ,