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对象及它的 ...
随机推荐
- HDU5406---CRB and Apple( DP) 2015 Multi-University Training Contest 10
题意比较简单, dp[i][j] 表示上一次男女吃的deliciousness分别为i, j的时候的吃的最多的苹果. 那么dp[i][j] = max(dp[i][k] + 1), 0 < ...
- 深度优先搜索-linux上浅显易懂的例子
上次看啊哈算法中的深度优先搜索,自己用的是linux(linux粉,windows黑,嘿嘿),字符界面,为了强化对这个的理解,就在linux上对这个例子的代码做了一点修改可以很清楚的看到整个搜索过程, ...
- redis在Java web项目的简单应用(转载)
看到一篇关于redis和spring集成的文章,实际测试后,可以.转载以备用.谢谢 亲昵YY! html,body { font-size: 15px } body { font-family: He ...
- 模型类中 Parcelable 接口使用
package com.exmyth.ui.model; import java.util.ArrayList; import java.util.List; public class Product ...
- linux命令详解--tcpdump
工作中一直在用tcpdump,感觉非常方便,今天心血来潮百度了一下tcpdump的用法,才发现原来还有这么多强大的功能自己都不知道,那叫一个汗啊. 以此文作为备份,记录一些新知道的用法,各位网友谁有新 ...
- struts2,hibernate,spring整合笔记(3)
struts2,hibernate,spring整合笔记(1) struts2,hibernate,spring整合笔记(2) 配好struts和hibernate就要开始spring了 老规矩,还是 ...
- C#导出数据到Excel通用的方法类
导出数据到Excel通用的方法类,请应对需求自行修改. 资源下载列表 using System.Data; using System.IO; namespace IM.Common.Tools { p ...
- 几个常用的ps命令
1. ps aux If you are looking for a short summary of the active processes, use ps aux [root@rhel7 tm ...
- IKAnalyzer原理分析
IKAnalyzer原理分析 IKAnalyzer自带的 void org.wltea.analyzer.dic.Dictionary.disableWords(Collection<Strin ...
- zendstudio正则匹配查询
Ctrl+H之后,显示的File Search标签页为Containing text. Alt+/ 帮助提示正则匹配的语法. 例子如下: select type from table where id ...