CVS导出&&自定义Attribute的使用
1.cvs导出:List转为byte[]
/// <summary>
/// CvsExport帮助类
/// </summary>
public static class CvsExportHelper
{
/// <summary>
/// Creates the CSV from a generic list.
/// </summary>;
/// <typeparam name="T"></typeparam>;
/// <param name="list">The list.</param>;
public static byte[] CreateCSVFromGenericList<T>(List<T> list)
{
if (list == null || list.Count == ) return null; //get type from 0th member
Type t = list[].GetType();
string newLine = Environment.NewLine;
MemoryStream output = new MemoryStream();
using (var sw = new StreamWriter(output, new UTF8Encoding(true)))// 用true来指定包含bom
{
//make a new instance of the class name we figured out to get its props
object o = Activator.CreateInstance(t);
//gets all properties
PropertyInfo[] props = o.GetType().GetProperties(); //foreach of the properties in class above, write out properties
//this is the header row
foreach (PropertyInfo pi in props)
{
var attributes = pi.GetCustomAttributes(false);
var columnMapping = attributes
.FirstOrDefault(a => a.GetType() == typeof(CSVColumnAttribute));
if (columnMapping != null)
{
var mapsto = columnMapping as CSVColumnAttribute;
sw.Write(mapsto.Name + ",");
}
else
{
sw.Write(pi.Name + ",");
}
}
sw.Write(newLine); //this acts as datarow
foreach (T item in list)
{
//this acts as datacolumn
foreach (PropertyInfo pi in props)
{
//this is the row+col intersection (the value)
string whatToWrite =
Convert.ToString(item.GetType()
.GetProperty(pi.Name)
.GetValue(item, null))
.Replace(',', ' ') + ','; sw.Write(whatToWrite); }
sw.Write(newLine);
}
sw.Flush();
}
byte[] bytesInStream = output.ToArray(); // simpler way of converting to array
output.Close();
return bytesInStream;
}
}
2.前端调用(网页:内存导出)
/// <summary>
/// CSV Export
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
[HttpPost]
[ActionName("CSVExport")]
public ActionResult CSVExport(ExportDSVRequest r)
{
string fileName = string.Format("CRMInfo-{0}.csv", Guid.NewGuid().ToString());
byte[] result = null;
if (string.IsNullOrWhiteSpace(r.JsonData)) {
return Redirect("~/report/list");//返回上一页
} var billsResult = JsonHelper.DeserializeJsonToList<AccountDailyMarginCSV>(r.JsonData); result = CvsExportHelper.CreateCSVFromGenericList(billsResult);
return File(result, "text/csv", fileName);
}
导出至特定路径
string fileName = string.Format("CRMInfo-{0}.csv", Guid.NewGuid().ToString());
string content = log.ToString();
string dir = Directory.GetCurrentDirectory();
string fullName = Path.Combine(dir, fileName);
if (File.Exists(fullName)) File.Delete(fullName);
using (FileStream fs = new FileStream(fullName, FileMode.CreateNew, FileAccess.Write))
{
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.Flush();
sw.Write(content);
sw.Flush();
sw.Close();
}
自定义attribute
/// <summary>
/// CSV列名Attribute
/// </summary>
public class CSVColumnAttribute : Attribute
{
/// <summary>
/// Name
/// </summary>
public string Name { get; }
/// <summary>
/// set name
/// </summary>
/// <param name="name"></param>
public CSVColumnAttribute(string name)
{
this.Name = name;
}
}
attribute使用:对List<T>中的T设置列名
CVS导出&&自定义Attribute的使用的更多相关文章
- XsdGen:通过自定义Attribute与反射自动生成XSD
前言 系统之间的数据交互往往需要事先定义一些契约,在WCF中我们需要先编写XSD文件,然后通过自动代码生成工具自动生成C#对象.对于刚刚接触契约的人来说,掌握XMLSpy之类的软件之后确实比手写XML ...
- 2.C#自定义Attribute
阅读目录 一:C#自定义Attribute 二:AttributeUsageAttribute中的3个属性(Property)中的AttributeTargets 三:Attribut ...
- 自定义Attribute 服务端校验 客户端校验
MVC 自定义Attribute 服务端校验 客户端校验/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Autho ...
- C#自定义Attribute值的获取与优化
C#自定义Attribute值的获取是开发中会经常用到的,一般我们的做法也就是用反射进行获取的,代码也不是很复杂. 1.首先有如下自定义的Attribute [AttributeUsage(Attri ...
- .net c#获取自定义Attribute
前言: 在c#开发中,有时候我们需要读取 Attribute中的信息(关于Attribute , 我自己把他理解成一个可以为类,属性标记的东西,这个标记可以为你提供一些关于类,方法,属性的额外信息) ...
- 【Q2D】如何导出自定义C++类给框架使用
本文基于Quick cocos2d x这个游戏框架,为了行文流畅,后面都简称Q2D 导出自定义c++类给项目使用已经有了现成的例子了 详见:http://quick.cocos.org/?p=235 ...
- 转:C#制作ORM映射学习笔记一 自定义Attribute类
之前在做unity项目时发现只能用odbc连接数据库,感觉非常的麻烦,因为之前做web开发的时候用惯了ORM映射,所以我想在unity中也用一下ORM(虽然我知道出于性能的考虑这样做事不好的,不过自己 ...
- 【MVC 笔记】MVC 自定义 Attribute 属性中的猫腻
原想在 MVC Action 上加一个自定义 Attribute 来做一些控制操作,最先的做法是在自定 Attribute 中定义一个属性来做逻辑判断,可惜事与愿违,这个属性值居然会被缓存起来,于是于 ...
- 通过自定义Attribute及泛型extension封装数据验证过程
需求来源: 在日常工作中,业务流程往往就是大量持续的数据流转.加工.展现过程,在这个过程中,不可避免的就是数据验证的工作.数据验证工作是个很枯燥的重复劳动,没有什么技术含量,需要的就是对业务流程的准确 ...
随机推荐
- 理解SQL SERVER中的逻辑读,预读和物理读
转自:https://www.cnblogs.com/CareySon/archive/2011/12/23/2299127.html 在我的上一篇关于SQL SERVER索引的博文,有圆友问道关于逻 ...
- PHP主动断开与浏览器的连接
以前整理过一篇<关于PHP连接处理中set_time_limit().connection_status()和ignore_user_abort()深入解析>,是解说浏览器client断开 ...
- 2.深度学习中的batch_size的理解
Batch_Size(批尺寸)是机器学习中一个重要参数,涉及诸多矛盾,下面逐一展开. 首先,为什么需要有 Batch_Size 这个参数? Batch 的选择,首先决定的是下降的方向.如果数据集比较小 ...
- (转)Linux Oracle服务启动&停止脚本与开机自启动
在CentOS 6.3下安装完Oracle 10g R2,重开机之后,你会发现Oracle没有自行启动,这是正常的,因为在Linux下安装Oracle的确不会自行启动,必须要自行设定相关参数,首先先介 ...
- Centos7 Zabbix3.2集群安装
安装环境:服务器10.80.0.191作为zabbix-server,10.80.0.191-195作为zabbix-agent. [zabbix@miyan ~]$ cat /etc/redhat- ...
- 尝试.Net Core—使用.Net Core + Entity FrameWork Core构建WebAPI(一)
想尝试.Net Core很久了,一直没有时间,今天回家,抛开一切,先搭建一个.Net Core的Demo出来玩玩. 废话少说,咱直奔主题: 一.开发环境 VS2015 Update3 Microsof ...
- EXTJS4扩展实例:如何使用filter查询treepanel
我们在使用普通的store时,extjs提供了filterBy,filter等多种方法来过滤数据达到查询效果,但在treepanel中的streeStore却没有实现这个查询,于是,就有了这篇文章. ...
- #C++初学记录(算法2)
A - Game 23 Polycarp plays "Game 23". Initially he has a number n and his goal is to trans ...
- 【codenet】代码相似度计算框架调研 -- 把内容与形式分开
首发于我的gitpages博客 https://helenawang.github.io/2018/10/10/代码相似度计算框架调研 代码相似度计算框架调研 研究现状 代码相似度计算是一个已有40年 ...
- Object-C-系统类型对象归档
系统类型主要是指NSString NSDictionary,NSArray,NSData,NSNumber 类型数据(包括对应可变类型); 这些类型已经实现了NSCoding协议,支持归档, 写入方法 ...