C#对CSV文件读写操作
CSV,百度解释如下:
逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。
结构如下:
TEST,STATUS,VALUE,U_LIMIT,L_LIMIT
SN,0,001915E9B8A2,NA,NA
STATION ID,0,Connectivity Station,NA,NA
APP START,0,0,0,0
SRW Engine Version : 2.8.0.4 <2016.8.1.1600>,0,0,0,0
Instrument Control DLL Version : 2.8.0.4 <2016.8.1.1600>,0,0,0,0
CSV文件和Excel文件结构非常类似,也和datatable的数据类型结构,这两种文件和datatable的数据在数据交互时比较直观。
首先介绍读写CSV文件,后续整理后总结出CSV,Excel的数据在与数据库交互时,用datatable数据类型的demo
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.IO; 7 8 namespace CsvFileOperator 9 { 10 class Program 11 { 12 public static void Main(string[] args) 13 { 14 string path = @"C:\Users\wuxinlong\Desktop\demo\DemoTest\testFile\CsvFileOperator"; 15 List<string[]> ls = new List<string[]>(); 16 //testdata 17 string[] str1 = { "TEST", "STATUS", "VALUE", "U_LIMIT", "L_LIMIT" }; 18 string[] str2 = { "SN", "0001915E9B8A2", "NA", "NA" }; 19 string[] str3 = { "STATION ID", "0", "Connectivity Station", "NA", "NA " }; 20 string[] str4 = { "APP START", "0", "0", "0", "0" }; 21 string[] str5 = { "SRW Engine Version : 2.8.0.4 <2016.8.1.1600>", "0", "0", "0", "0" }; 22 ls.Add(str1); ls.Add(str2); ls.Add(str3); ls.Add(str4); ls.Add(str5); 23 WriteCSV(path + "\\Test.csv", true, ls); 24 25 //以流的方式读取文件,将每行数据存放在数组里面,再将数组存放在list<string[]>集合里面 26 ls = ReadCSV(path + "\\Test.csv"); 27 //ls里面做两次循环,可以取到单独的数据 28 29 //直接读取每行的数据 30 string[] lines = File.ReadAllLines(path + "\\Test.csv"); //读取每行的数据,每行的数据放在字符串数组中 31 string test=null; string status=null; string value=null; string u_limit=null; string l_limit=null; 32 for (int i = 0; i < lines.Count(); i++) 33 { 34 test= lines[i].Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries)[0]; 35 status = lines[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[1]; 36 value = lines[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[2]; 37 u_limit = lines[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[3]; 38 l_limit = lines[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[4]; 39 string show = null; 40 show = test + "@" + status + "@" + value + "@" + u_limit + "@" + l_limit; 41 Console.WriteLine(show); 42 } 43 Console.ReadKey(); 44 } 45 46 public static void WriteCSV(string filePathName,bool append,List<string[]> ls) 47 { 48 StreamWriter fileWriter = new StreamWriter(filePathName, append, Encoding.Default); 49 foreach (string[] strArr in ls) 50 { 51 fileWriter.WriteLine(string.Join(",", strArr)); 52 } 53 fileWriter.Flush(); 54 fileWriter.Close(); 55 56 //使用using()的结构会自动清理流的缓冲区,并关闭流 57 //using(StreamWriter fileWriterbuff = new StreamWriter(filePathName,append,Encoding.Default)) 58 //{ 59 // foreach (string[] strArr in ls) 60 // { 61 // fileWriterbuff.WriteLine(string.Join(",",strArr)); 62 // } 63 // fileWriterbuff.Flush();//将缓冲区的数据写入流中,并清理缓冲区 64 //} 65 } 66 67 public static List<string[]> ReadCSV(string filePathName) 68 { 69 List<string[]> ls = new List<string[]>(); 70 StreamReader fileReader = new StreamReader(filePathName); 71 string strLine = ""; 72 while (strLine != null) 73 { 74 strLine = fileReader.ReadLine(); 75 if (strLine != null && strLine.Length > 0) 76 { 77 ls.Add(strLine.Split(',')); 78 Console.WriteLine(strLine); 79 } 80 } 81 fileReader.Close(); 82 return ls; 83 } 84 } 85 }