excel 内容粘贴到datagridview

 

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns.Add("FIELD_NAME", "字段名");
            dataGridView1.Columns.Add("FIELD_DESC", "字段描述");
            dataGridView1.Columns.Add("FIELD_TYPE", "字段类型");
            dataGridView1.Columns.Add("FIELD_LEN", "长度");
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string pasteText = Clipboard.GetText();
                if (string.IsNullOrEmpty(pasteText))
                {
                    return;
                }

                string[] lines = pasteText.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);               
                foreach (string line in lines)
                {                   
                    string[] strs = line.Split(new char[] { '\t' });                                       
                    dataGridView1.Rows.Add(strs);
                }
            }
            catch
            {
                //
            }           
        }


              
            //DataTable dt = new DataTable();     
            //dt.Columns.Add("FIELD_NAME", typeof(string));
            //dt.Columns.Add("FIELD_DESC", typeof(string));
            //dt.Columns.Add("FIELD_TYPE", typeof(string));
            //dt.Columns.Add("FIELD_LEN", typeof(Int32));
            //dataGridView1.DataSource = dt;
            //try
            //{
            //    string pasteText = Clipboard.GetText();
            //    if (string.IsNullOrEmpty(pasteText))
            //    {
            //        return;
            //    }

            //    string[] lines = pasteText.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            //    foreach (string line in lines)
            //    {
            //        string[] strs = line.Split(new char[] { '\t' });
            //        dt.Rows.Add(strs);
            //        //DataGridView控件被数据绑定时,无法以编程方式向DataGridView的行集合中添加行
            //        //dataGridView1.Rows.Add(strs );
            //    }
            //    dt.AcceptChanges();
            //}
            //catch
            //{
            //    //
            //}

 

转载于:https://www.cnblogs.com/abenz/archive/2010/10/12/1848663.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值