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
}
}

最新文章

  1. 初步认识html以及表格的制作
  2. 通过硬编码获取dubbo服务对象
  3. 【原】js检测移动端横竖屏
  4. android Activity基类通用方法
  5. 提升 LaTeX 效率的小工具:Detexify LaTeX handwritten symbol recognition
  6. 【转】【异常处理】Incorrect string value: &#39;\xF0\x90\x8D\x83...&#39; for column... Emoji表情字符过滤的Java实现
  7. LCA算法倍增算法(洛谷3379模板题)
  8. SQL语句修改表字段名/修改字段长度/增加字段/删除字段
  9. [GE]手动截取当前活动窗口,并且按规则命名(1/2)
  10. 【转】Warning: mysql_connect(): mysqlnd cannot connect to MySQL 4.1+ using the old insecure authenticat
  11. sum() over() 函数的使用
  12. ViewPager 详解(五)-----使用Fragment实现ViewPager滑动
  13. S3C3440看门狗驱动程序
  14. Customize Spring @RequestParam Deserialization for Maps and/or Nested Objects
  15. EXT2/EXT3文件系统(一)
  16. ActionBarSherlock,SlidingMenu
  17. [前端]Emmet 基本语法快查
  18. php无法连接mysql问题解决方法总结
  19. AnjularJS表单回车提交事件
  20. ArcEngine临时数据存储 创建内存工作空间

热门文章

  1. 解读sample5
  2. 在linux下文件转码
  3. SRM 584 DIV1
  4. Linux 上使用 Gmail SMTP 服务器发送邮件通知
  5. F - Truck History - poj 1789
  6. 微信/易信公共平台开发(二):自定义菜单的PHP实现(提供源码)
  7. 使用MJRefresh遇到的坑
  8. 新版福昕阅读器(Foxit Reader)启动速度慢解决办法
  9. Android应用程序之间共享文字和图片(一)
  10. MSSQL查询所有数据库表,指定数据库的字段、索引