zoukankan      html  css  js  c++  java
  • .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);//调用方法

  • 相关阅读:
    AI编辑SVG格式的相关问题
    HTML里的id等属性命名需要注意
    canvas绘图动画细节
    触控获取坐标判断滑动方向
    CSS3卡片旋转效果
    使用CURL下载远程文件保存到服务器
    微信JS-SDK应用DEMO
    布局转换:文档流->绝对定位
    ThinkPHP缓存微信公众号access_token
    JAVA JSP笔记
  • 原文地址:https://www.cnblogs.com/jeff151013/p/11739335.html
Copyright © 2011-2022 走看看