用FileHelplers导出csv数据:

[DelimitedRecord(",")]
[IgnoreEmptyLines()]
[ConditionalRecord(RecordCondition.ExcludeIfMatchRegex, "^,+$")]
public class FormItemExport
{
[FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string Id; [FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string FirstName; [FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string LastName; [FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string Email; [FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string RewardsCardHolder; [FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string CreateDatetime; } public class Utils
{
public delegate V ExportFill<T, V>(T inptOj); public static FormItemExport FillForm(WscLandingFormEntity le)
{
FormItemExport lre = new FormItemExport(); lre.Id = le.Id.ToString();
lre.FirstName = le.FirstName;
lre.LastName = le.LastName;
lre.Email = le.Email; if (le.RewardsCardHolder)
{
lre.RewardsCardHolder = "Yes";
}
else
{
lre.RewardsCardHolder = "No";
} lre.CreateDatetime = le.CreateDatetime.ToString("MM/dd/yyyy hh:mm:ss"); return lre;
} public static void Export<T, V>(EntityCollectionBase<T> ec, ExportFill<T, V> expF, string filename, string[] fieldsHeaders) where T : EntityBase
{
string delimiter = ",";
string qualifier = """; List<V> recordCollection = new List<V>();
foreach (T obj in ec)
{
recordCollection.Add(expF(obj));
} MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
ms.Position = 0; FileHelperEngine engine = new FileHelperEngine(typeof(V));
V[] erc = recordCollection.ToArray(); StringBuilder strOut = new StringBuilder(); for (int i = 0; i < fieldsHeaders.Length; i++)
{
strOut.Append(qualifier);
strOut.Append(fieldsHeaders[i]);
strOut.Append(qualifier);
if (i < fieldsHeaders.Length – 1)
strOut.Append(delimiter);
} engine.HeaderText = strOut.ToString();
engine.WriteStream(sw, erc); HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.AddHeader("content-disposition", "attachment; filename=" + filename);
response.ContentType = "application/vnd.ms-excel"; sw.Flush();
ms.Flush();
ms.WriteTo(response.OutputStream); ms.Close();
response.Flush();
response.End();
}
}

导出时:

Utils.Export < MclCommercialContactEntity, CommercialContactExport>
(list,new Utils.ExportFill<MclCommercialContactEntity,CommercialContactExport>(CommercialContactExport.ExportCommercialContacts), "Commercial Contacts.csv",
new string[] { "Organization", "First Name", "Last Name", "Address", "City", "Province", "Country", "Postal Code", "Phone Number", "Alternate Phone", "Email" });

FileHelpers 用法 z的更多相关文章

  1. 文件读取方法(FileHelpers) z

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using FileHelp ...

  2. XtraGrid的若干种用法 z

    支持多种类型的数据集合作为数据源 XtraGrid与传统的DataGridView一样,支持多种类型作为其数据源.下面例子是将DataTable, List<T>和数组分别绑定到XtraG ...

  3. DevExpress控件XtraGrid的Master-Detail用法 z

    XtraGrid支持Master-Detail展示,在自带的Demo中展示了一个“公司——产品——订单”的例子.自己照着实现了一下,有几处关键地方补充一下. 示例: 部门信息(主1)——部门下用户(从 ...

  4. sql with as 用法-Z

    以下内容转自:http:.com/ 一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个 ...

  5. DEV控件之ChartControl用法 z

    一.总体概述 这个控件包含3层,最外面的chartControl层.中间的XYDiagram层.最里面的Series层.功能非常强大,但同时使用起来也相对复杂,需要各个层之间相互协调设置才能达到自己想 ...

  6. C#并行库(TaskParallelLibrary)用法 z

    1. Task.Factory.StartNew(() => DoSomeWork());是异步的 下面的代码会先输出ddd,因为Task.Factory.Startnew不阻塞: var ta ...

  7. Matlab数字信号处理

    产生方波 clear t=0:0.01:10; subplot(4,1,1) f1=square(t);                       %  产生周期为2pi的方波信号 plot(t,f ...

  8. Python的Numpy库简述

    numpy 是 python 的科学计算库import numpy as np 1.使用numpy读取txt文件 # dtype = "str":指定数据格式 # delimite ...

  9. linux备忘录-shell脚本

    知识 shell执行方式 shell执行方式有 通过source或. 在现在的bash环境中执行脚本 变量等会保持 通过bash shell.sh或sh shell.sh 使用一个新的bash环境执行 ...

随机推荐

  1. kerl build error

    删除 archives文件夹就行了

  2. 关于cin 与 cout 的加速

    在用cin 与 cout 的时候 可以使用 ios::sync_with_stdio(); cin.tie(); cout.tie(); 这样在输入大数据的时候可以加快许多

  3. BZOJ - 2440 容斥定理

    组合枚举n/i/i,贡献为miu倍 /*H E A D*/ int mu[maxn],prime[maxn],cnt; bool isprime[maxn]; void sai(int n){ mu[ ...

  4. git平台创建项目(码云)

    一.在码云创建项目 1.新建仓库 2,项目名称等 3.创建后的界面 4.克隆远程项目到本地(项目地址和用户名密码) git clone 仓库地址 注意,如果当前目录下出现git仓库同名目录时,会克隆失 ...

  5. enum学习

    https://www.cnblogs.com/hyl8218/p/5088287.html

  6. ansile 命令解释选项

    1, -a MODULE_ARGS --args=MODULE_ARGS 作用传递参数给模块使用 2, --ask-vault-pass 执行时询问vault的密码 3, -B SECONDS --b ...

  7. Linux 后台运行程序 和切换至前台

    fg 将后台中的命令调至前台继续运行 jobs查看当前有多少在后台运行的命令 ctrl + z可以将一个正在前台执行的命令放到后台,并且暂停

  8. Apache Beam的目标

    不多说,直接上干货! Apache Beam的目标 统一(UNIFIED) 基于单一的编程模型,能够实现批处理(Batch processing).流处理(Streaming Processing), ...

  9. Apache Beam中的函数式编程理念

    不多说,直接上干货! Apache Beam中的函数式编程理念 Apache Beam的编程范式借鉴了函数式编程的概念,从工程和实现角度向命令式妥协. 编程的领域里有三大流派:函数式.命令式.逻辑式. ...

  10. oled屏幕配套取字模软件使用

    oled屏幕配套取字模软件使用 作者:李剀 出处:https://www.cnblogs.com/kevin-nancy/p/10531368.html欢迎转载,但也请保留上面这段声明.谢谢! **P ...