C#读取txt文件返回DATATABLE 您所在的位置:网站首页 winform读取txt C#读取txt文件返回DATATABLE

C#读取txt文件返回DATATABLE

2024-05-21 17:27| 来源: 网络整理| 查看: 265

//1.打开资源管理器

OpenFileDialog open = new OpenFileDialog();

  if (open.ShowDialog() == DialogResult.OK)            

{              

   textBox1.Text =open.FileName;       

 }

//传入txt文件路径参数 读取txt文件所有内容 返回DATATABLE

 public DataTable GetTxt(string pths)        {            StreamReader sr = new StreamReader(pths, Encoding.GetEncoding("GB2312"));            string txt = sr.ReadToEnd().Replace("\r\n", "-");            string[] nodes = txt.Split('-');            DataTable dt = new DataTable();            dt.Columns.Add("ID", typeof(string));            foreach (string node in nodes)            {                string[] strs = node.Split('-');                DataRow dr = dt.NewRow();                dr["ID"] = strs[0];                dt.Rows.Add(dr);            }            return dt;        }



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有