一個讀寫csv檔案的C#類

來源:互聯網
上載者:User

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這裡有一個比較好的參考。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.