当前位置: 首页 > 工具软件 > FileHelpers > 使用案例 >

FileHelpers 用法

公西繁
2023-12-01

官方地址:

http://www.filehelpers.com/

 

快速开始:

http://www.filehelpers.com/quick_start.html

 

优点,容易上手,用法简单。

支持,文件,Excel,Sql Server,Access等等。你也可以自己实现自己的数据库连接方式。

 

例子,使用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" });

  

 

over.

转载于:https://www.cnblogs.com/nevernet/archive/2010/04/22/1718023.html

 类似资料: