CSV文件读写

很多情况下都会用到CSV的文件读写,比如两个系统做数据交换。本文提供了一个完整的CSV读写类,可以方便的进行读写操作。

CSVHelper

using System;
 using System.Collections.Generic;
 using System.Text;
 using System.IO;
 
namespace CSV
 {
  public abstract class CSVHelper
  {
   protected string[] _heads = null;
   protected const int MaxBuffer = 65535;
   protected StringBuilder strB;
   protected string _fileName;
   protected const char _colChar = ',';
   protected const string _rowsChar = "\r\n";
   protected const char _rowsSplit = '\r';
   protected FileInfo _fi = null;
   private object _sync = null;
 
  public CSVHelper(string fileName, string[] heads)
   {
    if (!string.IsNullOrEmpty(fileName))
    {
     strB = new StringBuilder(MaxBuffer);
     _heads = heads;
     _fileName = fileName;
     _sync = new object();
 
    if (heads != null)
     {
      AppendList(heads);
     }
 
    _fi = new FileInfo(fileName);
    }
    else
    {
     throw new ArgumentException("参数错误");
    }
   }
 
  private void AppendList(string[] list)
   {
    lock (_sync)
    {
     int index = 0;
 
    foreach (string head in list)
     {
      strB.Append(head);
 
     index++;
 
     if (index == list.Length)
      {
       strB.Append(_rowsChar);
      }
      else
      {
       strB.Append(_colChar);
      }
     }
 
    //如果大于MaxBuffer 写入磁盘
     if (strB.Length >= MaxBuffer)
     {
      Save();
     }
    }
   }
 
  public void Append(string[] values)
   {
    if (_heads == null || (_heads != null && values.Length == _heads.Length))
    {
     AppendList(values);
    }
    else
    {
     throw new Exception("head的条目数和value的条目数不一样");
    }
   }
 
  public void Save()
   {
    if (!_fi.Exists)
    {
     FileStream fs = _fi.Create();
 
    fs.Close();
 
    fs.Dispose();
    }
 
   using (StreamWriter sw = _fi.AppendText())
    {
     sw.Write(strB.ToString());
 
    sw.Close();
 
    sw.Dispose();
    }
 
   strB.Remove(0, strB.Length);
   }
  }
 }

CSVReader

using System;
 using System.Collections.Generic;
 using System.Text;
 using System.IO;
 
namespace CSV
 {
  public class CSVReader : CSVHelper
  {
   private FileStream _fs;
   private int _curLocation;
   private int _length;
 
  /// <summary>
   /// 
  /// </summary>
   /// <param name="fileName">需要读取的完全路径</param>
   /// <param name="heads">头,可以为null</param>
   public CSVReader(string fileName, string[] heads)
    : base(fileName, heads)
   {
    _curLocation = 0;
    _fs = _fi.OpenRead();
    _length = (int)_fs.Length;
   }
 
  public string[] ReadLine()
   {
    while (true)
    {
     if (_fs.ReadByte() == _rowsSplit)
     {
      int start = _curLocation;
      int end = (int)_fs.Position - 1;
      _fs.Position = _curLocation;
      int bufferLength = end - start;
      byte[] buffer = new byte[bufferLength];
 
     StringBuilder sb = new StringBuilder(bufferLength);
 
     _fs.Read(buffer, 0, end - start);
 
     sb.Append(Encoding.UTF8.GetString(buffer));
 
     _curLocation = end + 2;
      _fs.Position = _curLocation;
 
     if (sb.Length > 0)
      {
       return sb.ToString().Split(_colChar);
      }
     }
     //没找到
     if (_fs.Position >= (_length - 1))
     {
      break;
     }
    }
 
   if (_fs != null)
    {
     _fs.Close();
     _fs.Dispose();
    }
 
   return null;
   }
  }
 }


CSVWritter

using System;
 using System.Collections.Generic;
 using System.Text;
 using System.IO;
 
namespace CSV
 {
  public class CSVWriter : CSVHelper
  {
   /// <summary>
   /// 
  /// </summary>
   /// <param name="fileName">写入的完全路径</param>
   /// <param name="heads">需要写入的头,可以为null</param>
   public CSVWriter(string fileName, string[] heads)
    : base(fileName, heads)
   {
    if (File.Exists(fileName))
    {
     File.Delete(fileName);
    }
   }
  }
 }


 

 

posted on 2012-09-04 16:51  小黑混北京  阅读(1173)  评论(0编辑  收藏  举报