泛型List导出Excel

今天整了个Excel泛型导出,切出来大家看看
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Linq;
InBlock.gif using System.Text;
InBlock.gif using System.Reflection;
InBlock.gif namespace Utility.Util
InBlock.gif{
InBlock.gif         public class Ecel<T>
InBlock.gif        {
InBlock.gif                 public static void DataBindTitleExcel(System.Web.HttpResponseBase response, List<T> list, List< string> columnName, List< string> propertyName, string ExcelTitle, string strUserMsg)
InBlock.gif                {
InBlock.gif
InBlock.gif                         if (list.Count == 0)
InBlock.gif                        {
InBlock.gif                                response.Write( "<script>alert('对不起,没有查询到任何记录,导出失败!')</script>");
InBlock.gif                                response.End();
InBlock.gif                        }
InBlock.gif                        response.ContentEncoding = Encoding.GetEncoding( "GB2312");
InBlock.gif                        response.ContentType = "application/ms-excel";
InBlock.gif                        response.AppendHeader( "Content-Disposition", "attachment;filename=Export.xls");
InBlock.gif                         int count = list.Count;
InBlock.gif                        StringBuilder builder = new StringBuilder();
InBlock.gif                        builder.Append( "<html><head>\n");
InBlock.gif                        builder.Append( "<meta http-equiv=\"Content-Language\" content=\"zh-cn\">\n");
InBlock.gif                        builder.Append( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\n");
InBlock.gif                        builder.Append( "</head>\n");
InBlock.gif                        builder.Append( "<table border=1>");
InBlock.gif                         if (ExcelTitle != "")
InBlock.gif                        {
InBlock.gif                                 string str = "<font size=4><b>" + ExcelTitle + "</b></font>";
InBlock.gif                                 if (strUserMsg != "")
InBlock.gif                                {
InBlock.gif                                        str = str + "(" + strUserMsg + ")";
InBlock.gif                                }
InBlock.gif                                builder.Append( string.Concat( new object[] { "<tr><td colspan=", count, ">", str, "</td></tr>" }));
InBlock.gif                        }
InBlock.gif                        builder.Append( "<tr><td colspan=" + count + " valign=middle height=24>");
InBlock.gif                        builder.Append( "查询时间:" + DateTime.Now.ToString( "G") + "</td></tr>");
InBlock.gif                        builder.Append( "<tr>\n");
InBlock.gif                         for ( int i = 0; i < columnName.Count; i++)
InBlock.gif                        {
InBlock.gif
InBlock.gif                                builder.Append( "<td bgcolor=#969696><b>" + columnName[i] + "</b></td>\n");
InBlock.gif
InBlock.gif                        }
InBlock.gif                        Type objType = typeof(T);
InBlock.gif                        BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static; //反射标识
InBlock.gif                        PropertyInfo[] propInfoArr = objType.GetProperties(bf);
InBlock.gif
InBlock.gif                         foreach (T model in list)
InBlock.gif                        {
InBlock.gif
InBlock.gif                                builder.Append( "<tr>");
InBlock.gif
InBlock.gif                                 foreach (PropertyInfo propInfo in propInfoArr)
InBlock.gif                                {
InBlock.gif
InBlock.gif                                         foreach ( string propName in propertyName)
InBlock.gif                                        {
InBlock.gif
InBlock.gif                                                 if ( string.Compare(propInfo.Name.ToUpper(), propName.ToUpper()) == 0)
InBlock.gif                                                {
InBlock.gif
InBlock.gif                                                        PropertyInfo modelProperty = model.GetType().GetProperty(propName);
InBlock.gif                                                         if (modelProperty != null)
InBlock.gif                                                        {
InBlock.gif
InBlock.gif                                                                 object objResult = modelProperty.GetValue(model, null);
InBlock.gif                                                                builder.Append( "<td style='vnd.ms-excel.numberformat:@'>" + ((objResult == null) ? string.Empty : objResult) + "</td>");
InBlock.gif                                                        }
InBlock.gif                                                         else
InBlock.gif                                                        {
InBlock.gif                                                                 throw new Exception( "Property name may be not exists!");
InBlock.gif                                                        }
InBlock.gif                                                }
InBlock.gif                                        }
InBlock.gif                                }
InBlock.gif                                builder.Append( "</tr>\n");
InBlock.gif                        }
InBlock.gif                        builder.Append( "</table>\n");
InBlock.gif                        response.Write(builder.ToString());
InBlock.gif                        response.End();
InBlock.gif                }
InBlock.gif
InBlock.gif        }
InBlock.gif}
看看效果,这是在MVC2中的导出
在Webforms中只要你将response换成System.Web.UI.Page page对象的Response即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值