码迷,mamicode.com
首页 > Windows程序 > 详细

C#读取txt文件返回DATATABLE

时间:2016-09-09 14:53:50      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

//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;
        }

C#读取txt文件返回DATATABLE

标签:

原文地址:http://www.cnblogs.com/-lxl/p/5856525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!