【学习笔记】 C#操作csv文件

本文介绍了CSVHelper类,包含方法如写入数据、读取全部内容、按行读取和修改CSV文件,以及删除指定行,提供对CSV文件的基本操作支持。
摘要由CSDN通过智能技术生成

##1. csv文件的操作

 class CSVHelper
 {
     //文件路径
     private string pathName;

     //list保存数据
     private List<string> msg;
     public CSVHelper(string path)
     {
         pathName = path;
         msg = new List<string>();
     }

     #region Method
     /// <summary>
   /// 写入数据
   /// </summary>
   /// <param name="data"></param>
    public void writeData(string data)
     {
         using (StreamWriter writer = new StreamWriter(pathName,true))
         {               
             writer.WriteLine($"{data}");
         }
     }
     /// <summary>
     /// 读取全部内容
     /// </summary>
     public List<string> readData()
     {
         using(StreamReader reader = new StreamReader(pathName))
         {
             // 读取文件的每一行
             while (!reader.EndOfStream)
             {
                 // 读取一行数据
                 string line = reader.ReadLine();
                 msg.Add(line);
             }
         }
         return msg;
     }
     /// <summary>
     /// 读取指定行
     /// </summary>
     /// <param name="num"></param>
     /// <returns></returns>
     public List<string> readLineData(int num)
     {
         List<string> str = new List<string>();
         using (StreamReader reader = new StreamReader(pathName))
         {
             int sum = 0;
             // 读取文件的每一行
             while (!reader.EndOfStream)
             {
                 sum++;
                 string line = reader.ReadLine();
                 if (sum == num)
                 {
                     str.Add(line);
                 }
             }
         }          
         return str;
     }
     /// <summary>
     /// 删除指定行
     /// </summary>
     /// <param name="num"></param>
     public void deleteLineData(int num)
     {
         List<string> str = new List<string>();
         using (StreamReader reader = new StreamReader(pathName))
         {
             // 读取文件的每一行
             while (!reader.EndOfStream)
             {
                 string line = reader.ReadLine();
                 str.Add(line);                              
             }
         }
            if(num >= 1 && num <= str.Count())
             {
                 str.RemoveAt(num-1);
             }
   
         using(StreamWriter writer = new StreamWriter(pathName))
         {
             foreach (string line in str)
             {
                 writer.WriteLine(line);
             }
            
         }
     }
     /// <summary>
     /// 修改数据
     /// </summary>
     /// <param name="str"></param>
     /// <param name="num"></param>
     public void updateData(string str,int num)
     {
         List<string> str1 = new List<string>();
         using (StreamReader reader = new StreamReader(pathName))
         {
             // 读取文件的每一行
             while (!reader.EndOfStream)
             {
                 string line = reader.ReadLine();
                 str1.Add(line);
             }
         }
         if (num >= 1 && num <= str1.Count())
         {
             str1[num - 1] = str;
         }            
         using (StreamWriter writer = new StreamWriter(pathName))
         {
             foreach (string line in str1)
             {
                 writer.WriteLine(line);
             }
         }
        
     }
     #endregion
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值