asp mvc 导出txt 文件泛型方法

public static void ExportFile<T>(T obj)

        {

 

            StringBuilder str = new StringBuilder();

 

            //列出obj 对象中的所有属性

            System.Reflection.PropertyInfo[] properties = obj.GetType().GetProperties();

 

            if (properties != null && properties.Length > 0)

            {

                // 局部变量 用来判断循环次数,输出换行和逗号

                int j = 0;

 

                foreach (var item in properties)

                {

                    // 获取属性值

                    object objvalue = item.GetValue(obj, null);

                    //是否是泛型类型

                    if (item.PropertyType.IsGenericType)

                    {

 

                        Type objType = objvalue.GetType();

                        // 获取泛型集合总数

                        int count = Convert.ToInt32(objType.GetProperty("Count").GetValue(objvalue, null));

                        // 遍历集合

                        for (int i = 0; i < count; i++)

                        {

 

                            object listitem = objType.GetProperty("Item").GetValue(objvalue, new object[] { i });

 

                            System.Reflection.PropertyInfo[] myPros = listitem.GetType().GetProperties();

                            // 局部变量 用来判断循环次数,输出换行和逗号

                            int k = 0;

                            // 遍历集合中的属性

                            foreach (var m in myPros)

                            {

                                 // 属性名

                                //str.Append(m.Name);

                                //str.Append(",");

                                //str.Append("\t");

                                if (m.GetValue(listitem, null) != null)

                                {

                                    // 属性值

                                    str.Append(m.GetValue(listitem, null));

                                }

                                else

                                {

                                    str.Append("空值");

                                }

                                // 换行

                                if ((k+1) % 2 == 0)

                                {

                                    str.Append("\n");

                                }

                                // 输出 逗号

                                else if (k % 2 == 0)

                                {

                                    str.Append(",");

                                }

                                k++;

                            }

                            }

                    }

 

                     // 非泛型类型

                    else

                    {

                        // 属性名

                        //str.Append(item.Name);

                        //str.Append(",");

                        //str.Append("\t");

 

                        //判断属性值

                        if (item.GetValue(obj, null) != null)

                        {

                            // 属性值

                            str.Append(item.GetValue(obj, null));

                        }

                        else

                        {

                            str.Append("空值");

                        }

                        // 换行

                        if ((j+1) % 2 == 0)

                        {

                            str.Append("\n");

                        }

                        // 输出逗号

                        else if (j % 2 == 0)

                        {

                            str.Append(",");

                        }

                        j++;

                    }

                }

            }

 

            HttpContext.Current.Response.Clear();

            // 启用缓存

            HttpContext.Current.Response.Buffer = true;

            //中文编码

            HttpContext.Current.Response.Charset = "GB2312";   //  或者 "utf-8"

            // 设置编码方式

            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

 

            //文件名称

            string filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt";

 

            /// 设置http 请求头,直接指向文件

            HttpContext.Current.Response.AddHeader("Content-Disposition",

                "attachment;filename=" + HttpContext.Current.Server.UrlEncode(filename));

 

            //指定返回的是一个不能被客户端读取的流,必须被下载     

            HttpContext.Current.Response.ContentType = "text/plain"; // 或者 application/ms-txt

 

            //把文件流发送到客户端

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

            // 停止页面的执行

            HttpContext.Current.Response.End();

        }
 
  因时间仓促,代码 也有不足之处,如果您有什么好的想法,欢迎提出并与我交流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值