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. python ord()与chr()用法以及区别

    ord()函数主要用来返回对应字符的ascii码,chr()主要用来表示ascii码对应的字符他的输入时数字,可以用十进制,也可以用十六进制. >>> ord("a&quo ...

  2. composer 安装yii2 The package is not available in a stable-enough version解决办法

    错误 Potential causes: - A typo in the package name - The package is not available in a stable-enough ...

  3. Feign的使用

    一.Feign实现应用间的通信 声明式REST客户端(伪RPC),采用基于接口的注解.本质上是Http客户端,Http远程调用. 1. 在Order工程中的pom文件增加 <dependency ...

  4. VMware Ubuntu如何连接互联网

    Brigde——桥接 :默认使用VMnet0  1.原理:  Bridge 桥”就是一个主机,这个机器拥有两块网卡,分别处于两个局域网中,同时在”桥”上,运行着程序,让局域网A中的所有数据包原封不动的 ...

  5. Linux之poll机制分析

    应用程序访问1个设备文件时可用阻塞/非阻塞方式.如果是使用阻塞方式,则直接调用open().read().write(),但是在驱动程序层会判断是否可读/可写,如果不可读/不可写,则将当前进程休眠,直 ...

  6. 总线读写---verilog代码

    总线读写---verilog代码 `timescale 1ns / 1ps ////////////////////////////////////////////////////////////// ...

  7. php输出异常的检查方法

    同事在用php展示验证码时,老是无法正常显示 但是的代码在别的地方正常运行 查看了输出,是有内容的,但是就是不显示 根据经验,应该是输出的内容有混淆,就是说在输出图片内容的时候,掺杂进去额外的信息 通 ...

  8. MySQL 5.7 优化SQL提升100倍执行效率的深度思考(GO)

    系统环境:微软云Linux DS12系列.Centos6.5 .MySQL 5.7.10.生产环境,step1,step2是案例,精彩的剖析部分在step3,step4. 1.慢sql语句大概需要13 ...

  9. Emacs的一些事情(与Vi的争议及使用)

    一年成为Emacs高手(像神一样使用编辑器)推荐文章 http://ftp.gnu.org/gnu/emacs/windows/http://blog.csdn.net/redguardtoo/art ...

  10. Sql Server Report Service 的部署问题(Reporting Service 2014為什麼不需要IIS就可以運行)

    http://www.cnblogs.com/syfblog/p/4651621.html Sql Server Report Service 的部署问题 近期在研究SSRS部署问题,因为以前也用到过 ...