csv内存流文件流
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web; namespace BFF.ExportData
{
public class ExportFile
{
#region Export
public static MemoryStream StreamToCSV<T>(IEnumerable<T> dataSource, string fileName)
where T : class
{
var result = string.Empty; //Header
StringBuilder headerRow = new StringBuilder();
var properties = typeof(T).GetProperties().Select(p => new
{
PropertyInfo = p,
HeaderAttr = p.GetFirstCustomAttribute<ExportHeaderAttribute>() as ExportHeaderAttribute
})
.Where(p => p.HeaderAttr != null)
.OrderBy(p => p.HeaderAttr.Order)
.ThenBy(p => p.PropertyInfo.Name)
.ToList(); var propertiesCount = properties.Count(); for (int i = ; i < propertiesCount; i++)
{
if (i < propertiesCount - )
headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(",");
else
headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(Environment.NewLine);
} //Data
StringBuilder fields = new StringBuilder();
foreach (var item in dataSource)
{
if (item == null)
continue;
for (int i = ; i < propertiesCount; i++)
{
var propertyValue = properties[i].PropertyInfo.GetValue(item, null);
var displayPropertyValue = propertyValue == null ? string.Empty : propertyValue.ToString();
if (i < propertiesCount - )
formatStringToCSVForm(fields, displayPropertyValue, false);
else
formatStringToCSVForm(fields, displayPropertyValue, true);
}
fields.Append(Environment.NewLine);
}
//build
result = headerRow.ToString() + fields.ToString();
//return result; byte[] bytetxt = Encoding.UTF8.GetBytes(result);
MemoryStream memstream = new MemoryStream();
memstream.Write(bytetxt, , bytetxt.Length);
memstream.Seek(, SeekOrigin.Begin);
return memstream;
} public static void ExportToCSV<T>(IEnumerable<T> dataSource, string fileName)
where T : class
{
var result = string.Empty; //Header
StringBuilder headerRow = new StringBuilder();
var properties = typeof(T).GetProperties().Select(p => new
{
PropertyInfo = p,
HeaderAttr = p.GetFirstCustomAttribute<ExportHeaderAttribute>() as ExportHeaderAttribute
})
.Where(p => p.HeaderAttr != null)
.OrderBy(p => p.HeaderAttr.Order)
.ThenBy(p => p.PropertyInfo.Name)
.ToList(); var propertiesCount = properties.Count(); for (int i = ; i < propertiesCount; i++)
{
if (i < propertiesCount - )
headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(",");
else
headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(Environment.NewLine);
} //Data
StringBuilder fields = new StringBuilder();
foreach (var item in dataSource)
{
if (item == null)
continue;
for (int i = ; i < propertiesCount; i++)
{
var propertyValue = properties[i].PropertyInfo.GetValue(item, null);
var displayPropertyValue = propertyValue == null ? string.Empty : propertyValue.ToString();
if (i < propertiesCount - )
formatStringToCSVForm(fields, displayPropertyValue, false);
else
formatStringToCSVForm(fields, displayPropertyValue, true);
}
fields.Append(Environment.NewLine);
}
//build
result = headerRow.ToString() + fields.ToString();
//return result; byte[] bytetxt = Encoding.UTF8.GetBytes(result);
FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
f.Write(bytetxt,,bytetxt.Length);
f.Close();
} private static void formatStringToCSVForm(StringBuilder sb, string field, bool isLast)
{ if (string.IsNullOrEmpty(field))
sb.Append(",");
else
{
sb.AppendFormat("\"{0}\"", field.Replace("\"", "\"\""));
if (!isLast)
sb.Append(",");
}
}
#endregion
}
}
csv内存流文件流的更多相关文章
- 节点流(文件流) FileInputStream & FileOutputStream & FileReader & FileWriter
节点流(文件流) FileInputStream(字节流)处理视频类的 FileOutputStream(字节流) FileReader(字符流)处理文本文件 ...
- 用内存流 文件流 资源生成客户端(Delphi开源)
正文:很多木马生成器就是用的内存流和文件流生成客户端的,废话不多说了,代码如下: unit Main; interface usesWindows, Messages, SysUtils, Varia ...
- io流-文件流\节点流
FileOutputStream类(jdk1.0) 描述 java.io.FileOutputStream 类是文件字节输出流,用于将数据写入到文件中. 构造方法 //构造方法 FileOutputS ...
- 文件流FileStram类
本节课主要学习三个内容: 创建FileStram流 读取流 写入流 文件流FileStram类,是用来实现对文件的读取和写入.FileStram是操作字节的字节数组,当提供向文件读取和写入字节的方法时 ...
- C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)
一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@&qu ...
- .NET客户端下载SQL Server数据库中文件流保存的大电子文件方法(不会报内存溢出异常)
.NET客户端下载SQL Server数据库中文件流保存的大电子文件方法(不会报内存溢出异常) 前段时间项目使用一次性读去SQL Server中保存的电子文件的文件流然后返回给客户端保存下载电子文件, ...
- -1-4 java io java流 常用流 分类 File类 文件 字节流 字符流 缓冲流 内存操作流 合并序列流
File类 •文件和目录路径名的抽象表示形式 构造方法 •public File(String pathname) •public File(String parent,Stringchild) ...
- .net 流(Stream) - 文件流、内存流、网络流
转自:http://www.oseye.net/user/kevin/blog/85 一.文件流 FileStream FileStream流继承与Stream类,一个FileStream类的实例实际 ...
- js实现使用文件流下载csv文件
1. 理解Blob对象 在Blob对象出现之前,在javascript中一直没有比较好的方式处理二进制文件,自从有了Blob了,我们就可以使用它操作二进制数据了.现在我们开始来理解下Bolb对象及它的 ...
随机推荐
- bzoj4097 [Usaco2013 dec]Vacation Planning
Description Air Bovinia is planning to connect the N farms (1 <= N <= 200) that the cows live ...
- UVAlive3523 Knights of the Round Table(bcc)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18122 [思路] 点-双连通分量 求出bcc,判断每个bcc是否为 ...
- C++ Primer Plus(第6版)中文版——课后练习程序代码
博客内容经历了一次整理,以前发的博文太散.没什么水准,搞的随笔分类越来越多orz,这次把CPP这本书的课后练习的程序代码放到一起方便查阅与修改..嗯 9.6.1 #ifndef _9..1_H_ #d ...
- [工作] 使在家办公(Work From Home)更有效率的建议
在家办公(Work From Home) 是一种灵活的工作方式,节省了漫长的上下班挤公交的时间,有更多可支配的时间. 我司允许员工申请在家办公,每周一天的配额.本人长期在办公室工作,习惯了办公室里工作 ...
- 大规模Hadoop集群在腾讯数据仓库TDW的实践
随着业务的快速增长,TDW的节点数也在增加,对单个大规模Hadoop集群的需求也越来越强烈.TDW需要做单个大规模集群,主要是从数据共享.计算资源共享.减轻运营负担和成本等三个方面考虑. 数据共享.T ...
- L - Connections in Galaxy War - zoj 3261
题意:有一个帝国在打仗,敌方会搞一些破坏,总共用N个阵地,每个阵地都有一个武力值,当第一地方收到攻击的时候他可以进行求助,当然求助的对象只能是武力值比他高的,如果求助失败就输出 ‘-1’, 求助成功就 ...
- WKWebview点击图片查看大图
大家都知道,WKWebview是没有查看大图的属性或者方法的,所以只能通过js与之交互来实现这一功能,原理:通过js获取页面的图片,把它存放到数组,给图片添加点击事件,通过index显示大图就行了 其 ...
- Ubuntu+Win7+Samba实现文件共享
Samba是Ubuntu和Windows进行网络共享的工具,比如分享打印机,互相之间传输资料文件. 安装samba sudo apt-get install samba 查看samba是否安装成功 s ...
- Disposable microfluidic devices: fabrication, function, and application Gina S. Fiorini and Daniel T
Disposable microfluidic devices: fabrication, function, and application Gina S. Fiorini and Daniel T ...
- Linux 高可用(HA)集群之keepalived详解
http://freeloda.blog.51cto.com/2033581/1280962 大纲 一.前言 二.Keepalived 详解 三.环境准备 四.LVS+Keepalived 实现高可用 ...