C#读写csv文件
1 public class CSVFileHelper 2 { 3 /// <summary> 4 /// 将DataTable中数据写入到CSV文件中 5 /// </summary> 6 /// <param name="dt">提供保存数据的DataTable</param> 7 /// <param name="fileName">CSV的文件路径</param> 8 public static void SaveCSV(DataTable dt, string fullPath) 9 { 10 FileInfo fi = new FileInfo(fullPath); 11 if (!fi.Directory.Exists) 12 { 13 fi.Directory.Create(); 14 } 15 FileStream fs = new FileStream(fullPath, System.IO.FileMode.Create, System.IO.FileAccess.Write); 16 //StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); 17 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); 18 string data = ""; 19 //写出列名称 20 for (int i = 0; i < dt.Columns.Count; i++) 21 { 22 data += dt.Columns[i].ColumnName.ToString(); 23 if (i < dt.Columns.Count - 1) 24 { 25 data += ","; 26 } 27 } 28 sw.WriteLine(data); 29 //写出各行数据 30 for (int i = 0; i < dt.Rows.Count; i++) 31 { 32 data = ""; 33 for (int j = 0; j < dt.Columns.Count; j++) 34 { 35 string str = dt.Rows[i][j].ToString(); 36 str = str.Replace("\"", "\"\"");//替换英文冒号 英文冒号需要换成两个冒号 37 if (str.Contains(',') || str.Contains('"') 38 || str.Contains('\r') || str.Contains('\n')) //含逗号 冒号 换行符的需要放到引号中 39 { 40 str = string.Format("\"{0}\"", str); 41 } 42 43 data += str; 44 if (j < dt.Columns.Count - 1) 45 { 46 data += ","; 47 } 48 } 49 sw.WriteLine(data); 50 } 51 sw.Close(); 52 fs.Close(); 53 DialogResult result = MessageBox.Show("CSV文件保存成功!"); 54 if (result == DialogResult.OK) 55 { 56 System.Diagnostics.Process.Start("explorer.exe", Common.PATH_LANG); 57 } 58 } 59 60 /// <summary> 61 /// 将CSV文件的数据读取到DataTable中 62 /// </summary> 63 /// <param name="fileName">CSV文件路径</param> 64 /// <returns>返回读取了CSV数据的DataTable</returns> 65 public static DataTable OpenCSV(string filePath) 66 { 67 Encoding encoding = Common.GetType(filePath); //Encoding.ASCII;// 68 DataTable dt = new DataTable(); 69 FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); 70 71 //StreamReader sr = new StreamReader(fs, Encoding.UTF8); 72 StreamReader sr = new StreamReader(fs, encoding); 73 //string fileContent = sr.ReadToEnd(); 74 //encoding = sr.CurrentEncoding; 75 //记录每次读取的一行记录 76 string strLine = ""; 77 //记录每行记录中的各字段内容 78 string[] aryLine = null; 79 string[] tableHead = null; 80 //标示列数 81 int columnCount = 0; 82 //标示是否是读取的第一行 83 bool IsFirst = true; 84 //逐行读取CSV中的数据 85 while ((strLine = sr.ReadLine()) != null) 86 { 87 //strLine = Common.ConvertStringUTF8(strLine, encoding); 88 //strLine = Common.ConvertStringUTF8(strLine); 89 90 if (IsFirst == true) 91 { 92 tableHead = strLine.Split(','); 93 IsFirst = false; 94 columnCount = tableHead.Length; 95 //创建列 96 for (int i = 0; i < columnCount; i++) 97 { 98 DataColumn dc = new DataColumn(tableHead[i]); 99 dt.Columns.Add(dc); 100 } 101 } 102 else 103 { 104 aryLine = strLine.Split(','); 105 DataRow dr = dt.NewRow(); 106 for (int j = 0; j < columnCount; j++) 107 { 108 dr[j] = aryLine[j]; 109 } 110 dt.Rows.Add(dr); 111 } 112 } 113 if (aryLine != null && aryLine.Length > 0) 114 { 115 dt.DefaultView.Sort = tableHead[0] + " " + "asc"; 116 } 117 118 sr.Close(); 119 fs.Close(); 120 return dt; 121 } 122 }
转https://blog.csdn.net/oSKyTonight/article/details/47301851