日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 将List中的数据导入csv文件中

發布時間:2024/4/17 C# 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 将List中的数据导入csv文件中 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

//http://www.cnblogs.com/mingmingruyuedlut/archive/2013/01/20/2849906.html

?

C# 將List中的數據導入csv文件中

將數據保存至文件中,是一個比較常用的功能,數據源可以是多種形式,文件也可以是多種。

這里簡單的介紹將List數據導入到CSV文件中的方法。

代碼如下所示:

Student類:

public class Student{private string id;public string Id { get { return id; } set { id = value; } }private string name;public string Name { get { return name; } set { name = value; } }private string age;public string Age { get { return age; } set { age = value; } }}

?

模擬一個簡單的List數據源:

private List<Student> GetStudentData(){List<Student> studentList = new List<Student>();Student s1 = new Student();s1.Id = "1";s1.Name = "haha";s1.Age = "10";Student s2 = new Student();s2.Id = "2";s2.Name = "xixi";s2.Age = "20";Student s3 = new Student();s3.Id = "3";s3.Name = "lolo";s3.Age = "30";studentList.Add(s1);studentList.Add(s2);studentList.Add(s3);return studentList;}

?

根據文件路徑創建相應的文件:

/// <summary>/// Create target file/// </summary>/// <param name="folder">folder</param>/// <param name="fileName">folder name</param>/// <param name="fileExtension">file extension</param>/// <returns>file path</returns>private string CreateFile(string folder, string fileName, string fileExtension){FileStream fs = null;string filePath = folder + fileName + "." + fileExtension;try{if (!Directory.Exists(folder)){Directory.CreateDirectory(folder);}fs = File.Create(filePath);}catch (Exception ex){ }finally{if (fs != null){fs.Dispose();}}return filePath;}

?

獲取類的屬性集合(以便生成CSV文件的所有Column標題):

private PropertyInfo[] GetPropertyInfoArray(){PropertyInfo[] props = null;try{Type type = typeof(EricSunApp.Student);object obj = Activator.CreateInstance(type);props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);}catch (Exception ex){ }return props;}

?

對List進行遍歷,將數據導入CSV文件中(宗旨就是在一行數據中以逗號進行分割):

/// <summary>/// Save the List data to CSV file/// </summary>/// <param name="studentList">data source</param>/// <param name="filePath">file path</param>/// <returns>success flag</returns>private bool SaveDataToCSVFile(List<Student> studentList, string filePath){bool successFlag = true;StringBuilder strColumn = new StringBuilder();StringBuilder strValue = new StringBuilder();StreamWriter sw = null;PropertyInfo[] props = GetPropertyInfoArray();try{sw = new StreamWriter(filePath);for(int i = 0; i < props.Length; i++){strColumn.Append(props[i].Name);strColumn.Append(",");}strColumn.Remove(strColumn.Length - 1, 1);sw.WriteLine(strColumn); //write the column namefor(int i = 0; i < studentList.Count; i++){strValue.Remove(0, strValue.Length); //clear the temp row valuestrValue.Append(studentList[i].Id);strValue.Append(",");strValue.Append(studentList[i].Name);strValue.Append(",");strValue.Append(studentList[i].Age);sw.WriteLine(strValue); //write the row value}}catch(Exception ex){successFlag = false;}finally{if (sw != null){sw.Dispose();}}return successFlag;}

?

簡單例舉具體的調用:

private bool EricSunExportData(string folder, string fileName, string fileExtension){List<Student> studentList = GetStudentData();string filePath = CreateFile(folder, fileName, fileExtension);bool flag = SaveDataToCSVFile(studentList, filePath);return flag;}

轉載于:https://www.cnblogs.com/TNSSTAR/p/4283855.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的C# 将List中的数据导入csv文件中的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。