C# 导出篇

导出

导出为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;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值