·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 将List下载到本地保存为Excel

将List下载到本地保存为Excel

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

将List下载到本地保存为Excel

  直接附上代码

 1     /// <summary> 2     /// 将List保存为Excel 3     /// </summary> 4     /// <typeparam name="T">保存的类类型</typeparam> 5     /// <param name="lt">需要保存的源数据</param> 6     /// <param name="fileName">保存的文件名称</param> 7     /// <param name="fields">对应于类的字段名称</param> 8     /// <param name="titles">对应于Excel的列名</param> 9     public static void Save<T>(List<T> lt, string fileName, string[] fields, string[] titles)10     {11         if (lt == null || lt.Count == 0)12         {13             throw new ArgumentNullException("数据为空");14         }15 16         var sb = new StringBuilder();17         PRopertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);18         const string next = "\t";19 20         foreach (var title in titles)21         {22             sb.Append(title).Append(next);23         }24 25         var propertys = new List<PropertyInfo>();26 27         foreach (var field in fields)28         {29             foreach (PropertyInfo prop in props)30             {31                 if (prop.Name.Equals(field))32                 {33                     propertys.Add(prop);34                     break;35                 }36             }37         }38 39         sb.Append(Environment.NewLine);40 41         foreach (T item in lt)42         {43             foreach (var property in propertys)44             {45                 object value = property.GetValue(item, null);46 47                 if (property.PropertyType.BaseType == typeof(Enum))48                 {49                     sb.Append(GPMSKernel.Unility.Enums.GetEnumDescription(value));50                 }51                 else if (property.PropertyType == typeof(Boolean))52                 {53                     if ((bool)value)54                     {55                         sb.Append("是");56                     }57                     else58                     {59                         sb.Append("否");60                     }61                 }62                 else63                 {64                     sb.Append(value);65                 }66 67                 sb.Append(next);68             }69 70             sb.Append(Environment.NewLine);71         }72 73         fileName = string.Format("{0}_{1}.xls", DateTime.Now.ToString("yyMMddHHmmss"), fileName);74         HttpContext.Current.Response.ContentType = "application/octet-stream";75         //通知浏览器下载文件而不是打开76         HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));77         HttpContext.Current.Response.BinaryWrite(Encoding.UTF8.GetBytes(sb.ToString()));78         HttpContext.Current.Response.Flush();79         HttpContext.Current.Response.End();80     }

   此方法是把List从Web服务器上下载到本地并保存为Excel的,其中lt是要保存的数据源,fileName是对应的文件名,可以直接写为要保存的类名,

fields对应于要保存的类的字段名,这个一定要正确,否则无法在类里找到该字段,titles是要保存的文件里面的列名,fields和titles必须一一对应,个数要相等。  保存之前有做特殊处理,比如如果要保存的类里面有枚举的话,直接保存到Excel里会变成英文的,这一般不是用户想看到的,那么需要在枚举的每个值前面标注[Description("***")],
GPMSKernel.Unility.Enums.GetEnumDescription可以获得枚举的Description值;如果是布尔值的话,直接保存就变成了"True"或"False"了,我把这种转换成了"是"或"否";如果要保存的类里面还有特殊的类,那么需要在这特殊的类里面重写ToString方法,保存的时候将会按照该ToString方法保存....