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时钟,计算程序运行时间

    关于计算程序执行时间 import time def sleep(): time.sleep(2.5) def forloop(count): for i in range(count): print ...

  2. Jmeter性能监测及安装插件(推荐)

    本文部分理论转自Jmeter官网:https://jmeter-plugins.org/wiki/PerfMon/  ,并结合个人实践编写 一.介绍 在负载测试期间,了解加载服务器的运行状况很重要.如 ...

  3. Visual Studio: 一键卸载所有组件工具,彻底卸载干净。

    第一步.手动卸载VS主体 第二步.下载工具并解压 网盘下载地址:https://pan.baidu.com/s/1eSHRYxW 也可以在Github上下载最新版本:https://github.co ...

  4. System Generator 使用离散资源

    System Generator 使用离散资源 重要,怎样配置FPGA中的DSP Macro 最后是编译模型

  5. bzoj4941: [Ynoi2016]镜子里的昆虫

    维护每个位置x的上一个相等的位置pv[x],可以把询问表示成l<=x<=r,pv[x]<l的形式,对一次修改,均摊改变O(1)个pv的取值,因此可以用平衡树预处理出pv的变化,用cd ...

  6. 1.Linux命令

    所有文章都只做学习记录用! 一.Linux开发板操作命令 1.查看命令:  系统相关:           **任务管理器: gnome-system-monitor 查看系统版 :uname -a ...

  7. MySQL面试题和答案

    Mysql 的存储引擎,myisam和innodb的区别. 答: 1.MyISAM 是非事务的存储引擎,适合用于频繁查询的应用.表锁,不会出现死锁,适合小数据,小并发. 2.innodb是支持事务的存 ...

  8. 浙江财经大学第十五届大学生程序设计竞赛------B 烦恼先生打麻将

    问题 B: B - 烦恼先生打麻将 时间限制: 1 Sec  内存限制: 256 MB提交: 8  解决: 5[提交][状态][讨论版] 题目描述 输入 6 6 Z D 1S 1S 9W 5W 2S ...

  9. jquery ajax的load()方法和load()事件

    1.使用 AJAX 请求来改变 div 元素的文本: $("button").click(function(){ $("div").load('demo_aja ...

  10. HDOJ 2019 数列有序!

    #include<vector> #include<iostream> #include<algorithm> #include<cstdio> usi ...