using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using System.Web; using System.IO; using System.Reflection; using System.Data; namespace Mamarow.SysUtils { public static class ExcelManager { /// <summary> /// 导出EXCEL通用方法 /// </summary> /// <typeparam name="T">实体类的泛型</typeparam> /// <param name="Re">Response对象</param> /// <param name="Name">到处的EXCEL名字</param> /// <param name="Li">数据内容,List列表</param> /// <param name="datas">存储列标题和列字段的字典,key标题,value字段</param> public static void Export<T>(HttpResponseBase Re,string Name,List<T> Li,Dictionary<string, string> datas) { StringWriter sw = new StringWriter(); StringBuilder HtmlStr = new StringBuilder(); StringBuilder HtmlCon = new StringBuilder(); HtmlStr.Append(@"<table cellpadding='0' cellspacing='0' style='width:auto'> <tr> <td valign='middle' style='height: 50px; font-size: 12px; font-weight: bold; color: #133F7E;border:solid 1px #133F7E' align='center' colspan='"); HtmlStr.Append(datas.Keys.Count()); HtmlStr.Append(@"'>"); HtmlStr.Append(HttpUtility.UrlEncode(Name)); HtmlStr.Append( @"</td> </tr> <tr>"); for(int i=0; i<datas.Keys.Count(); i++) { HtmlStr.Append(@" <td valign='middle' align='center' style='font-size: 12px; font-weight: bold; color: #4EAB1E; background-color: #D6EBCA;border:solid 1px #86C260'>"); HtmlStr.Append(datas.ToList()[i].Key.ToString()); HtmlStr.Append(@" </td> "); } HtmlStr.Append(@" </tr> "); #region ---内容 for (int j = 0; j < Li.Count; j++) { HtmlCon.Append(@" <tr> "); string value = ""; object oValue=null; for (int i = 0; i < datas.Keys.Count(); i++) { foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) { if (datas.ToList()[i].Value.ToString() == propertyInfo.Name) { oValue = propertyInfo.GetValue(Li[j], null); } if(oValue != null) value = oValue.ToString(); } HtmlCon.Append(@"<td valign='middle' align='left' style='font-size: 12px;border:solid 1px #86C260'>" + value + "</td>"); } HtmlCon.Append(@" </tr> "); } #endregion HtmlStr.Append(HtmlCon.ToString()); HtmlStr.Append(@"</table>"); sw.WriteLine(HtmlStr.ToString()); sw.Close(); Re.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name) + (new Random().Next())+".xls"); Re.ContentType = "application/ms-excel"; Re.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Re.Write(sw); Re.End(); } private static IList<T> GetEntityList<T>(IDataReader dr) where T : new() { IList<T> entityList = new List<T>(); int fieldCount = -1; while (dr.Read()) { if (-1 == fieldCount) fieldCount = dr.FieldCount; // 得到实体类对象 T t = (T)Activator.CreateInstance(typeof(T)); for (int i = 0; i < fieldCount; i++) { PropertyInfo prop = t.GetType().GetProperty(dr.GetName(i), BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); if (null != prop) { // 为了能用在默认为null的值上 // 如 DateTime? tt = null; if (null == dr[i] || Convert.IsDBNull(dr[i])) prop.SetValue(t, null, null); else prop.SetValue(t, dr[i], null); } } entityList.Add(t); } dr.Close(); return entityList; } } }