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; } } }