当前位置: 移动技术网 > IT编程>开发语言>c# > 一个读写csv文件的C#类

一个读写csv文件的C#类

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了一个读写csv文件的c#类,供大家参考,具体内容如下

using system;
using system.collections.generic;
using system.io;
using system.text;

namespace csvdemo
{
  /// <summary>
  /// csvutil is a helper class handling csv files.
  /// </summary>
  public class csvutil
  {
    private csvutil()
    {
    }
    //write a new file, existed file will be overwritten
    public static void writecsv(string filepathname,list<string[]>ls)
    {
      writecsv(filepathname,false,ls);
    }
    //write a file, existed file will be overwritten if append = false
    public static void writecsv(string filepathname,bool append, list<string[]> ls)
    {
      streamwriter filewriter=new streamwriter(filepathname,append,encoding.default);
      foreach(string[] strarr in ls)
      {
        filewriter.writeline(string.join (“,",strarr) );
      }
      filewriter.flush();
      filewriter.close();
      
    }
    public static list<string[]> readcsv(string filepathname)
    {
      list<string[]> ls = new list<string[]>();
      streamreader filereader=new  streamreader(filepathname); 
      string strline="";
      while (strline != null)
      {
        strline = filereader.readline();
        if (strline != null && strline.length>0)
        {
          ls.add(strline.split(','));
          //debug.writeline(strline);
        }
      } 
      filereader.close();
      return ls;
    }
    
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网