• .NET 泛型集合数据写CSV文件


    1.功能类

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;

    namespace Infrastructure
    {
        public static class FileExtensions
        {
            public static bool EnsureDirectory(string path)
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                return true;
            }

            public static bool CreateFile(string folder, string fileName, string fileExtension)
            {
                EnsureDirectory(folder);
                string filePath = Path.Combine(folder, string.Format("{0}.{1}",fileName, fileExtension));
                using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
                {}
                return true;
            }

            public static bool SaveToCsv<T>(IEnumerable<T> array, string filePath)
            {
                bool flag = true;
                try
                {
                    StringBuilder stringBuilderColumn = new StringBuilder();
                    StringBuilder stringBuilderValue = new StringBuilder();

                    Type type = typeof (T);
                    object obj = Activator.CreateInstance(type);
                    PropertyInfo[] props = type.GetProperties(BindingFlags.Public|BindingFlags.Instance);

                    props.ToList().ForEach(x => stringBuilderColumn.Append(string.Format("{0},",x.Name)));
                    stringBuilderColumn.Remove(stringBuilderColumn.Length - 1, 1);

                    using (StreamWriter streamWriter = new StreamWriter(filePath))
                    {
                        streamWriter.WriteLine(stringBuilderColumn);
                        array.ToList().ForEach(x =>
                        {
                            stringBuilderValue.Remove(0, stringBuilderValue.Length);
                            foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof (T)))
                            {
                                stringBuilderValue.Append(string.Format("{0},",pd.GetValue(x) == null ? pd.GetValue(x) : pd.GetValue(x).ToString().Replace(',', ',')));
                            }
                            stringBuilderValue.Remove(stringBuilderValue.Length - 1, 1);
                            streamWriter.WriteLine(stringBuilderValue);
                        });
                    }
                }
                catch
                {
                    flag = false;
                }
                return flag;
            }
        }
    }
    2.调用方式

    List<T> items = new List<T>();

    xxx //为items集合赋值

    string filePath = xxx;//文件存储路径

    FileExtensions.SaveToCsv(items,filePath);//调用方法

  • 相关阅读:
    爬取药智网中的方剂信息
    日报3.13
    数据库添加出错
    Bencode
    一些安全网络协议
    代码质量不重要
    Jordan Peterson
    随身记录的缺点
    Why is Go PANICking?
    go问
  • 原文地址:https://www.cnblogs.com/jeff151013/p/11739335.html
Copyright © 2020-2023  润新知