zoukankan      html  css  js  c++  java
  • C# 将List数据 导出到csv 文件

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Utilities.IO
    {
    
        /// <summary>
        /// 标记属性的别名Title
        /// </summary>
        public class AttrForCsvColumnLabel : Attribute
        {
            public string Title { get; set; }
        }
    
        public static class CsvFileUtility
        {
    
            /// <summary>
            /// Save the List data to CSV file
            /// </summary>
            /// <param name="dataList">data source</param>
            /// <param name="filePath">file path</param>
            /// <returns>success flag</returns>
            public static bool SaveDataToCSVFile<T>(List<T> dataList, string filePath) where T : class
            {
    
    
                bool successFlag = true;
    
                StringBuilder sb_Text = new StringBuilder();
                StringBuilder strColumn = new StringBuilder();
                StringBuilder strValue = new StringBuilder();
                StreamWriter sw = null;
                var tp = typeof(T);
                PropertyInfo[] props = tp.GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
                try
                {
                    //sw = new StreamWriter(filePath);
                    for (int i = 0; i < props.Length; i++)
                    {
                        var itemPropery = props[i];
                        AttrForCsvColumnLabel labelAttr = itemPropery.GetCustomAttributes(typeof(AttrForCsvColumnLabel), true).FirstOrDefault() as AttrForCsvColumnLabel;
                        if (null != labelAttr)
                        {
                            strColumn.Append(labelAttr.Title);
                        }
                        else
                        {
                            strColumn.Append(props[i].Name);
                        }
    
                        strColumn.Append(",");
                    }
                    strColumn.Remove(strColumn.Length - 1, 1);
                    //sw.WriteLine(strColumn);    
                    //write the column name
                    sb_Text.AppendLine(strColumn.ToString());
    
                    for (int i = 0; i < dataList.Count; i++)
                    {
                        var model = dataList[i];
                        //strValue.Remove(0, strValue.Length);
                        //clear the temp row value
                        strValue.Clear();
                        for (int m = 0; m < props.Length; m++)
                        {
                            var itemPropery = props[m];
                            var val = itemPropery.GetValue(model, null);
                            if (m == 0)
                            {
                                strValue.Append(val);
                            }
                            else
                            {
                                strValue.Append(",");
                                strValue.Append(val);
                            }
                        }
    
    
                        //sw.WriteLine(strValue); 
                        //write the row value
                        sb_Text.AppendLine(strValue.ToString());
                    }
                }
                catch (Exception ex)
                {
                    successFlag = false;
                }
                finally
                {
                    if (sw != null)
                    {
                        sw.Dispose();
                    }
                }
    
                File.WriteAllText(filePath, sb_Text.ToString(), Encoding.Default);
    
                return successFlag;
            }
        }
    }
    

      

  • 相关阅读:
    计网 | 文件传输协议
    Java | JDK8 | Integer
    2.项目管理-应用工具
    1.需求管理
    1.短视频运营基础
    10.视频效果---变形稳定器
    9.时间重映射
    8.效果控件之移动&&缩放
    7.标记
    6.子剪辑与合并剪辑
  • 原文地址:https://www.cnblogs.com/micro-chen/p/12372965.html
Copyright © 2011-2022 走看看