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内存流文件流的更多相关文章

  1. 节点流(文件流) FileInputStream & FileOutputStream & FileReader & FileWriter

    节点流(文件流) FileInputStream(字节流)处理视频类的                   FileOutputStream(字节流) FileReader(字符流)处理文本文件    ...

  2. 用内存流 文件流 资源生成客户端(Delphi开源)

    正文:很多木马生成器就是用的内存流和文件流生成客户端的,废话不多说了,代码如下: unit Main; interface usesWindows, Messages, SysUtils, Varia ...

  3. io流-文件流\节点流

    FileOutputStream类(jdk1.0) 描述 java.io.FileOutputStream 类是文件字节输出流,用于将数据写入到文件中. 构造方法 //构造方法 FileOutputS ...

  4. 文件流FileStram类

    本节课主要学习三个内容: 创建FileStram流 读取流 写入流 文件流FileStram类,是用来实现对文件的读取和写入.FileStram是操作字节的字节数组,当提供向文件读取和写入字节的方法时 ...

  5. C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)

    一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@&qu ...

  6. .NET客户端下载SQL Server数据库中文件流保存的大电子文件方法(不会报内存溢出异常)

    .NET客户端下载SQL Server数据库中文件流保存的大电子文件方法(不会报内存溢出异常) 前段时间项目使用一次性读去SQL Server中保存的电子文件的文件流然后返回给客户端保存下载电子文件, ...

  7. -1-4 java io java流 常用流 分类 File类 文件 字节流 字符流 缓冲流 内存操作流 合并序列流

      File类 •文件和目录路径名的抽象表示形式 构造方法 •public File(String pathname) •public File(String parent,Stringchild) ...

  8. .net 流(Stream) - 文件流、内存流、网络流

    转自:http://www.oseye.net/user/kevin/blog/85 一.文件流 FileStream FileStream流继承与Stream类,一个FileStream类的实例实际 ...

  9. js实现使用文件流下载csv文件

    1. 理解Blob对象 在Blob对象出现之前,在javascript中一直没有比较好的方式处理二进制文件,自从有了Blob了,我们就可以使用它操作二进制数据了.现在我们开始来理解下Bolb对象及它的 ...

随机推荐

  1. GOTO (Transact-SQL)

    将执行流更改到标签处. 跳过 GOTO 后面的 Transact-SQL 语句,并从标签位置继续处理. GOTO 语句和标签可在过程.批处理或语句块中的任何位置使用. GOTO 语句可嵌套使用. 语法 ...

  2. VMware Ubuntu安装详细过程

    参考链接: http://blog.csdn.net/u013142781/article/details/50529030

  3. SQL注入语句 (很全)

    1.返回的是连接的数据库名and db_name()>02.作用是获取连接用户名and user>03.将数据库备份到Web目录下面;backup database 数据库名 to dis ...

  4. CSS3实现文字描边

    -webkit-text-shadow:#000 1px 0 0,#000 0 1px 0,#000 -1px 0 0,#000 0 -1px 0; -moz-text-shadow:#000 1px ...

  5. android实现界面左右滑动(GridView动态设置item,支持每个item按某个属性排序来显示在不同的界面)

    效果图 :                         分别是第一页.第二页.第三页,随手截的图,不整齐,勿见怪.开始走了弯路,废了不少时间. 思路如下: 1.用ViewPager实现左右分页滑动 ...

  6. Coding Your Life

    前几天看到篇文章,写的是科技让人变得陌生,balabala,总的说来就科技让邻居是男是女不知道了,朋友见面少了之类的.其实我觉得,也不能全怪科技发展的太快,而是人心都飘到网路上了,像我这一辈已经老去的 ...

  7. linux中内核的一个不错的参数somaxconn

    导读:在linux中,/proc/sys/net/core/somaxconn这个参数,linux中内核的一个不错的参数somaxconn 看下其解析: 对于一个TCP连接,Server与Client ...

  8. 把数据库中的null作为条件查询应该用is

    如select * from mbXX where tuijian is null 而不是select * from mbXX where tuijian=null

  9. (转)select 1 from ... sql语句中的1代表什么意思? .

    select  1 from ..., sql语句中的1代表什么意思?查出来是个什么结果?         select 1 from table;与select anycol(目的表集合中的任意一行 ...

  10. .net中Web.config文件的基本原理及相关设置

    11.7  使用web.config配置文件 Web配置文件web.config是Web 应用程序的数据设定文件,它是一份 XML 文件,内含 Web 应用程序相关设定的 XML 标记,可以用来简化  ...