一个读写csv文件的C#类

[c-sharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Text;  
  5.   
  6. namespace CSVDemo  
  7. {  
  8.     /// <summary>  
  9.     /// CSVUtil is a helper class handling csv files.  
  10.     /// </summary>  
  11.     public class CSVUtil  
  12.     {  
  13.         private CSVUtil()  
  14.         {  
  15.         }  
  16.         //write a new file, existed file will be overwritten  
  17.         public static void WriteCSV(string filePathName,List<String[]>ls)  
  18.         {  
  19.             WriteCSV(filePathName,false,ls);  
  20.         }  
  21.         //write a file, existed file will be overwritten if append = false  
  22.         public static void WriteCSV(string filePathName,bool append, List<String[]> ls)  
  23.         {  
  24.             StreamWriter fileWriter=new StreamWriter(filePathName,append,Encoding.Default);  
  25.             foreach(String[] strArr in ls)  
  26.             {  
  27.                 fileWriter.WriteLine(String.Join (“,",strArr) );  
  28.             }  
  29.             fileWriter.Flush();  
  30.             fileWriter.Close();  
  31.               
  32.         }  
  33.         public static List<String[]> ReadCSV(string filePathName)  
  34.         {  
  35.             List<String[]> ls = new List<String[]>();  
  36.             StreamReader fileReader=new   StreamReader(filePathName);    
  37.             string strLine="";  
  38.             while (strLine != null)  
  39.             {  
  40.                 strLine = fileReader.ReadLine();  
  41.                 if (strLine != null && strLine.Length>0)  
  42.                 {  
  43.                     ls.Add(strLine.Split(','));  
  44.                     //Debug.WriteLine(strLine);  
  45.                 }  
  46.             }   
  47.             fileReader.Close();  
  48.             return ls;  
  49.         }  
  50.           
  51.     }  
  52. }  

另外:http://www.codeproject.com/KB/database/CsvReader.aspx这里有一个比较好的参考。


http://blog.csdn.net/gisfarmer/article/details/4533970

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值