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对象及它的 ...
随机推荐
- Java中BitSet使用(转)
java.util.BitSet,采用位运算: 官方API:http://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html 摘要: Bit ...
- cf591B Rebranding
B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- cloudCompute
云计算使企业可以迅速对市场做出反应而且加强了企业内部的协同 人人都在用云计算的时候能够迅速迁移到云平台已经不叫优势了,反而不使用云计算成为了一种劣势
- Git常用命令清单
创建 在指定目录创建工作仓库,未指定时在当前目录 git init [dir] 创建不包含工作区的仓库 git init --bare 克隆远程仓库到本地 git clone url 配置 git c ...
- SKPhysicsJointFixed类
继承自 NSObject 符合 NSCoding(SKPhysicsJoint)NSObject(NSObject) 框架 /System/Library/Frameworks/SpriteKit. ...
- [React] Radium: Updating Button Styles via Props
In a CSS library like Bootstrap we can set a button's style to be "primary" or "secon ...
- TinyXml高速入门(一)
作者:朱金灿 来源:http://blog.csdn.net/clever101 对于xml文件,眼下我的工作仅仅是集中在配置文件和作为简单的信息文件来用,因此我不太喜欢使用msxml这样的重量级的x ...
- Qt Sqlite封装类SqliteUtil
在网上找了很久关于Qt访问Sqlite数据库的封装类,但是没能找到一个很好的访问调用类,自己写了一个出来,在这里分享一下,希望能对大家有所帮助,小弟不才,写代码没多少经验,如果有什么不恰当之处,请批评 ...
- 【解决】WordPress FTP连接服务器时出错,请检查设置,WordPress需要访问您网页服务器的权限
刚装好wordpress,发现后台预装了两个插件,想删掉,结果要登录FTP,死活登不上去,提示"连接服务器时出错,请检查设置,WordPress需要访问您网页服务器的权限",网上也 ...
- web03--session
1.创建session1.jsp <body> <form action="session2.jsp" method="post"> & ...