zoukankan      html  css  js  c++  java
  • asp mvc 导出txt 文件泛型方法

     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("	");
                                    if (m.GetValue(listitem, null) != null)
                                    {
                                        // 属性值
                                        str.Append(m.GetValue(listitem, null));
                                    }
                                    else
                                    {
                                        str.Append("空值");
                                    }
                                    // 换行
                                    if ((k+1) % 2 == 0)
                                    {
                                        str.Append("
    ");
                                    }
                                    // 输出 逗号
                                    else if (k % 2 == 0)
                                    {
                                        str.Append(",");
                                    }
                                    k++;
                                }
                                }
                        }
    
                         // 非泛型类型
                        else
                        {
                            // 属性名
                            //str.Append(item.Name);
                            //str.Append(",");
                            //str.Append("	");
    
                            //判断属性值
                            if (item.GetValue(obj, null) != null)
                            {
                                // 属性值
                                str.Append(item.GetValue(obj, null));
                            }
                            else
                            {
                                str.Append("空值");
                            }
                            // 换行
                            if ((j+1) % 2 == 0)
                            {
                                str.Append("
    ");
                            }
                            // 输出逗号
                            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();
            }
    

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

  • 相关阅读:
    AVL自平衡二叉树
    笔试+面试信息整理----面向笔试学习、面向面经编程
    传入值参数&传入引用参数的区别
    NLPIR智能挖掘实现行业大数据应用价值
    NLPIR大数据挖掘融合库、智、理三大先进理论技术
    NLPIR智能语义挖掘文本大数据深层意义
    NLPIR智能语义技术从采集到分析一步到位
    灵玖软件:NLPIR智能挖掘对文本数据精细化分析
    九眼合同智能审核系统运用NLPIR大数据技术进行核查
    NLPIR九眼智能审核平台助合同文本深度核查
  • 原文地址:https://www.cnblogs.com/wisdo/p/3868067.html
Copyright © 2011-2022 走看看