简体   繁体   English

从Excel复制粘贴

[英]Copy Paste from Excel

I am copying data from excel and pasting it to the windows form with button click. 我正在从Excel复制数据,然后单击按钮将其粘贴到Windows窗体。

The copied data from excel is being split and populate the four textboxes. 从excel复制的数据被拆分并填充四个文本框。 All the texboxes are populated as expected but the last textbox get extra "\\r\\n". 所有的texbox都按预期填充,但最后一个文本框得到额外的“ \\ 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. 我抓取的变量看起来像这个测试\\ 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);

            }

        }

And here is the screen shot of break point. 这是断点的屏幕截图。

在此处输入图片说明

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);

Alternatively, you could use TrimEnd() to trim specific characters off the end of a string. 或者,您可以使用TrimEnd()修剪掉字符串末尾的特定字符。

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

I doubt there's a way to prevent it from being included on the ClipBoard when you copy from Excel. 我怀疑有一种方法可以防止从Excel复制时将其包含在ClipBoard中。

As you can't avoid \\t, you can't avoid getting \\r\\n. 由于无法避免\\ t,因此无法避免\\ r \\ n。 This is how the pasting client knows about tabular data using \\t \\r and \\n. 这就是粘贴客户端如何使用\\ t \\ r和\\ n了解表格数据的方式。

If you copy a Excel range which has more than one Rows, you will get many \\r\\n. 如果复制的Excel范围具有多个行,则会得到许多\\ r \\ n。

Best is to remove \\r\\n using String.Replace method. 最好是使用String.Replace方法删除\\ r \\ n。

New line within an Excel cell is the CR character, which is "\\r" in C#. Excel单元格中的新行是CR字符,在C#中为“ \\ r”。

As answered by GSerghttp . 正如GSerghttp回答的那样

You could remove the extra content by : 您可以通过以下方式删除多余的内容:

s.ToString().TrimEnd( '\r', '\n' );

or replace as : 或替换为:

text = text.Replace("\r\n", "");

Example: 例:

string OriginString = "\r\n\r\nText goes here\r\n";
string NewString = OriginString.Replace("\r\n", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM