using System;<br />using System.Collections.Generic;<br />using System.IO;<br />using System.Text;</p><p>namespace CSVDemo<br />{<br /> /// <summary><br /> /// CSVUtil is a helper class handling csv files.<br /> /// </summary><br /> public class CSVUtil<br /> {<br /> private CSVUtil()<br /> {<br /> }<br /> //write a new file, existed file will be overwritten<br /> public static void WriteCSV(string filePathName,List<String[]>ls)<br /> {<br /> WriteCSV(filePathName,false,ls);<br /> }<br /> //write a file, existed file will be overwritten if append = false<br /> public static void WriteCSV(string filePathName,bool append, List<String[]> ls)<br /> {<br /> StreamWriter fileWriter=new StreamWriter(filePathName,append,Encoding.Default);<br /> foreach(String[] strArr in ls)<br /> {<br /> fileWriter.WriteLine(String.Join (“,",strArr) );<br /> }<br /> fileWriter.Flush();<br /> fileWriter.Close();</p><p> }<br /> public static List<String[]> ReadCSV(string filePathName)<br /> {<br /> List<String[]> ls = new List<String[]>();<br /> StreamReader fileReader=new StreamReader(filePathName);<br /> string strLine="";<br /> while (strLine != null)<br /> {<br /> strLine = fileReader.ReadLine();<br /> if (strLine != null && strLine.Length>0)<br /> {<br /> ls.Add(strLine.Split(','));<br /> //Debug.WriteLine(strLine);<br /> }<br /> }<br /> fileReader.Close();<br /> return ls;<br /> }</p><p> }<br />}<br />
另外:http://www.codeproject.com/KB/database/CsvReader.aspx這裡有一個比較好的參考。