List导出数据Excel

using System.Collections.Generic;

using System.IO;

using System.Reflection;

using System.Web;

 

    /// <summary>

            /// Excle导出数据

            /// </summary>

            /// <typeparam name="T">类对象</typeparam>

            /// <param name="list">对象数据</param>

            /// <param name="column">类字段,字段对应列名</param>

            /// <param name="filename">excel表名</param>

            public void OutExcel<T>(List<T> list, Dictionary<string, string> column, string filename)

            {

                if (list == null || list.Count == 0 || column == null || column.Count == 0)

                {

                    return;

                }

                StringWriter sw = new StringWriter();

                //-------------------------------表头读取开始------------------------------------------------

                string title = string.Empty;

                foreach(KeyValuePair<string,string> kvp in column)

                {

                    title += kvp.Value + "/t";

                }

                title = title.Substring(0,title.LastIndexOf("/t"));

                sw.WriteLine(title);

                //-------------------------------表头读取结束--------------------------------------------------------

                //--------------------------------数据读取start----------------------------------------------------------------------------------

                Type objType = typeof(T);

                BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;//反射标识 

                PropertyInfo[] propInfoArr = objType.GetProperties(bf); //获取映射列表

                foreach (T model in list)

                {

                    System.Text.StringBuilder data = new System.Text.StringBuilder();

                    foreach (string key in column.Keys)

                    {

                        foreach (PropertyInfo propInfo in propInfoArr)

                        {

                            if (key == propInfo.Name)//判断头相对应的字段 

                            {

                                PropertyInfo modelProperty = model.GetType().GetProperty(propInfo.Name);

                                if (modelProperty != null)

                                {

                                    object objResult = modelProperty.GetValue(model, null);//获取值                        

                                    data.Append(((objResult == null) ? string.Empty : objResult) + "/t");

                                }

                            }

                        }

 

                    }

                    var temp= data.ToString();

                    temp = temp.Substring(0, temp.LastIndexOf("/t"));

                    sw.WriteLine(temp);

                }

                //------------------------------------------end----------------------------------------------------------------------------------

                sw.Close();//读取数据结束

                //-----------------------------------输出excel-------------------------------------------------------------

                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="+filename+".xls");

                HttpContext.Current.Response.ContentType = "application/ms-excel";

                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GBK");

                HttpContext.Current.Response.Write(sw.ToString());

                HttpContext.Current.Response.End();

                //-------------------------------------------------------------------------------------------------------------               

            }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值