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();
}

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

asp mvc 导出txt 文件泛型方法的更多相关文章

  1. ASP.NET MVC 导出CSV文件

    ASP.NET MVC   导出CSV文件.直接贴代码 /// <summary> /// ASP.NET MVC导出CSV文件Demo1 /// </summary> /// ...

  2. C# DataGirdview手动添加数据,导出txt文件并自动对齐

    //DataGirdview手动添加数据 private void btnDataGirdView_Click(object sender,EventArgs e) {       dataGridV ...

  3. ASP.NET 保存txt文件

    public void ProcessRequest(HttpContext context) { context.Response.Clear(); context.Response.Buffer ...

  4. asp.net 导出excel文件

    之前做过winfrom程序的导出excel文件的功能,感觉非常简单.现在试着做asp.net中导出excel的功能,之前用的是Microsoft.Office.Interop.Excel这个对象来实现 ...

  5. java读取txt文件,对字符串进行操作后导出txt文件

    嘿嘿,代码略为简单,不再多做解释,直接上码! package org.lq.com.util; import java.io.File; import java.io.InputStreamReade ...

  6. MVC C# 直接导出txt文件

    用asp.net根据数据内容自动生成一个txt文本文件并提供用户下载,此方法文件不保存在服务器上,直接提供给用户下载,到网上搜了一下,都是用的Response.BinaryWrite(),用了几下,发 ...

  7. ASP.NETCore -----导出Excel文件并下载

    本事例分为nopi(安装DotNetCore.NPOI)下载和EPPlus(EPPlus.Core.dll)下载,其中npoi下载演示的是根据执行的模板进行数据下载 npoi帮助类NpoiExcelU ...

  8. ASP.NET导出Excel文件

    第一种最常见,并且最简单的方式,直接把GridView导出,导出格式为文本表格形式. protected void btnSaveExcel_Click(object sender, EventArg ...

  9. ASP.NET导出bdf文件

    1.导出助手类 using System;using System.IO;using System.Data;using System.Data.OleDb;using System.Web;usin ...

随机推荐

  1. workerman的使用实践--并与solaris通信

    Workerman与solarisTCP通信测试 1.  笔记本win7,tcp_test.php 2.  solaris,test.c 编译命令: gcc test.c –o  test –lsoc ...

  2. java-plupload上传大文件

    参考链接: https://blog.csdn.net/hjf_1291030386/article/details/74784172 https://www.cnblogs.com/tengyunh ...

  3. Hadoop 完全分布式部署

    完全分布式部署Hadoop 分析: 1)准备3台客户机(关闭防火墙.静态ip.主机名称) 2)安装jdk 3)配置环境变量 4)安装hadoop 5)配置环境变量 6)安装ssh 7)集群时间同步 7 ...

  4. 单机RedHat6.5+JDK1.8+Hadoop2.7.3+Spark2.1.1+zookeeper3.4.6+kafka2.11+flume1.6环境搭建步骤

    1.RHEL 6.5系统安装配置图解教程(rhel-server-6.5) 2.在Linux下安装JDK图文解析 3.RedHat6.5上安装Hadoop单机 4.RedHat6.5安装Spark单机 ...

  5. SDRAM单字写操作

    SDRAM单字写操作 1.单字写操作时序 2.写verilog程序体会 在初始状态,先写好跳转条件.If()....else... 3.通过仿顺序操作来实现连续写操作 首先完成单字写操作,然后跳转到下 ...

  6. Excel技巧--使用规划求解

    当我们需要求在有限预算下可以购买的商品数量时,我们就可以使用“规划求解”功能.如上图,在1000元的预算目标内,我们能购买左图中的各书籍多少本.而这些数量,就可以使用“规划求解”来获取答案. 1.实际 ...

  7. NDK学习笔记(三):DynamicKnobs的机制

    最近的NDK开发涉及到了动态input及动态knobs的问题. 开发需求如下:建立一个节点,该节点能获取每一个input上游的inputframerange信息. 具体下来就是:需要Node的inpu ...

  8. google使用的开源的工具类Thumbnailator图像处理

    maven依赖 <dependency>     <groupId>net.coobird</groupId>     <artifactId>thum ...

  9. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

  10. C++进阶--静态多态

    //############################################################################ /* * 多态 */ // 常见的动态多态 ...