/// <summary> /// 写入数据到CSV文件,覆盖形式 /// </summary> /// <param name="csvPath">要写入的字符串表示的CSV文件</param> /// <param name="LineDataList">要写入CSV文件的数据,以string[]类型List表示的行集数据</param> public static void OpCsv(string csvPath, List<string[]> LineDataList) { using (FileStream fs = new FileStream(csvPath.Trim(), FileMode.OpenOrCreate, FileAccess.ReadWrite)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) { StringBuilder sb_csvStr = new StringBuilder(); for (int i = 0; i < LineDataList.Count; i++)//<--row { sb_csvStr.Clear(); for (int j = 0; j < LineDataList[i].Length; j++)//<--col { sb_csvStr.Append(string.Format("{0},", LineDataList[i][j].ToString())); } sw.WriteLine(sb_csvStr.ToString().Substring(0, sb_csvStr.ToString().Length - 1)); } fs.Flush(); } } }
/// <summary> /// 读取csv文件 /// </summary> /// <param name="csvPath">string 标识的 csv文件路径</param> /// <returns>返回的读取的 csv 文件的行集合</returns> public static string[] ReadCsv(string csvPath) { string[] csvLineList = File.ReadAllLines(csvPath, Encoding.Default); return csvLineList; }