从Excel复制粘贴 [英] Copy Paste from Excel

查看:94
本文介绍了从Excel复制粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从excel复制数据,然后单击按钮将其粘贴到Windows窗体中.

I am copying data from excel and pasting it to the windows form with button click.

从excel复制的数据被拆分并填充四个文本框.所有texbox均按预期填充,但最后一个文本框得到额外的"\ r \ n".它不会显示在文本框中,但是如果我放置断点并查看变量,则会有额外的\ r \ n.

The copied data from excel is being split and populate the four textboxes. All the texboxes are populated as expected but the last textbox get extra "\r\n". It doesn't show up in the textbox but if i put break point and look at the variable there is extra \r\n.

我抓取的变量看起来像这个测试\ r \ n.

The variable I am grabbing looks like this test\r\n.

我知道我可以将其替换为",但是有办法避免这种情况的发生.

I know I can replace this with "" but is there a way to avoid this happening.

这是我的粘贴事件处理程序的代码.

Here is the code for my paste event handler.

 if (Clipboard.GetText() != null)
            {
                string s = Clipboard.GetText();
                string[] lines = s.Split('\t');
                if (lines.Length < 3)
                {
                    DialogResult result = MsgBox.Show("Please copy valid data with tab space.", "Incorrect data format.", MsgBox.Buttons.OK, MsgBox.Icon.Exclamation, MsgBox.AnimateStyle.ZoomIn);
                    //MessageBox.Show("Please copy valid data with tab space.", "Incorrect data format.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }

                else
                {
                    green_textBox_ref.Text = lines[0].Replace(" ", "");
                    green_textBox1.Text = lines[1].Replace(" ", "");
                    green_textBox2.Text = lines[2].Replace(" ", "");
                    green_textBox3.Text = lines[3].Replace(" ", "");
                    green_textBox_ref.Enabled = false;
                    green_textBox1.Enabled = false;
                    green_textBox2.Enabled = false;
                    green_textBox3.Enabled = false;
                    paste_green.Enabled = false;
                }
            }

            else
            {
                DialogResult result = MsgBox.Show("There is no data to paste.", "No data", MsgBox.Buttons.OK, MsgBox.Icon.Error, MsgBox.AnimateStyle.ZoomIn);
                //MessageBox.Show("There is no data to paste.", "No data", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);

            }

        }

这是断点的屏幕截图.

推荐答案

您可以通过几种不同的方式删除该换行符.由于已经在分割字符串,因此可以告诉它将换行符作为附加的定界符,并删除空的子字符串.

You could remove that ending line newline a few different ways. Since you're already splitting the string, you could tell it to treat the newline as an additional delimiter, and remove empty substrings.

string[] lines =
    s.Split(new[] {"\t", Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);

或者,您可以使用 TrimEnd()从字符串末尾修剪特定的字符.

Alternatively, you could use TrimEnd() to trim specific characters off the end of a string.

green_textBox3.Text = lines[3].Replace(" ", "").TrimEnd('\r', '\n');

我怀疑从Excel复制时是否有一种方法可以防止它包含在ClipBoard中.

I doubt there's a way to prevent it from being included on the ClipBoard when you copy from Excel.

这篇关于从Excel复制粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆