LINQtoCSV 泛型数据转换为cvs

介绍

  • 使用LINQtoCSV.dll 将泛型数据转换为cvs
  • 自定义导出数据
 public static void DownFile(List<UserInfo> list,string ext)
        {


            CsvFileDescription outputFileDescription = new CsvFileDescription
            {
                SeparatorChar = ',', // tab delimited
                FirstLineHasColumnNames = true, // no column names in first record
                FileCultureName = "zh-cn" // use formats used in The Netherlands
            };

            CsvContext cc = new CsvContext();


            var cvsSavePath = "MemberFiles";
            var SavePatch = AppDomain.CurrentDomain.BaseDirectory + cvsSavePath;
            if (!Directory.Exists(SavePatch))
            {
                Directory.CreateDirectory(SavePatch);
            }

            string filename = DateTime.Now.ToString("yyyyMMdd")+"_"+ext+".csv";

            string fileNamePath = SavePatch + "\\" + filename;

            cc.Write(list, fileNamePath, outputFileDescription);
        }

  实体类

[CsvColumn(Name = "会员注册名", FieldIndex = 1)]
        public string MemberRegName { get; set; }
        [CsvColumn(Name = "登陆密码", FieldIndex = 2)]
        public string MemberPassword { get; set; }
        [CsvColumn(Name = "会员来源", FieldIndex = 3)]
        public string MemberSource { get; set; }
        [CsvColumn(Name = "会员等级", FieldIndex = 4)]
        public string MemberLevel { get; set; }
        [CsvColumn(Name = "会员卡号", FieldIndex = 5)]
        public string MemberNumber { get; set; }

  

posted @ 2013-05-21 23:15  21天后  阅读(393)  评论(0编辑  收藏  举报
21