• FileHelpers 用法 z


    用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" });
    
  • 相关阅读:
    [转]JAVA程序执行顺序,你了解了吗:JAVA中执行顺序,JAVA中赋值顺序
    [转]浅谈Java中的equals和==
    [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名
    [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器
    C# 数组之List<T>
    C# 数组之ArrayList
    C# 数组之int[]
    reverse-XNUCA-babyfuscator
    reverse-daily(1)-audio_visual_receiver_code
    Python多线程和多进程谁更快?
  • 原文地址:https://www.cnblogs.com/zeroone/p/4419551.html
Copyright © 2020-2023  润新知