【问题标题】:Is it possible to paste Excel / CSV data from clipboard to DataGridView in C#?是否可以将 Excel / CSV 数据从剪贴板粘贴到 C# 中的 DataGridView?
【发布时间】:2010-12-13 09:07:56
【问题描述】:

我使用 DataGridView 控件来管理一个简单的字典(几列和几百行)。 DataGridView 功能几乎就足够了。我可以添加新行、修改值并将数据从其中复制到 Excel。我不能做的一件事是将数据从 Excel 复制到我的控件。是否可以使用某些属性?还是需要一些代码才能做到这一点?

【问题讨论】:

    标签: c# .net excel datagridview csv


    【解决方案1】:

    这是从 codeprojet 文章中编辑的。略有不同 `

    private void PasteClipboard()
            {
                try
                {
                    string s = Clipboard.GetText();
                    string[] lines = s.Split('\n');
                    int iFail = 0, iRow = dgData.CurrentCell.RowIndex;
                    int iCol = dgData.CurrentCell.ColumnIndex;
                    DataGridViewCell oCell;
                    if (dgData.Rows.Count < lines.Length)
                        dgData.Rows.Add(lines.Length-1);
                    foreach (string line in lines)
                    {
                        if (iRow < dgData.RowCount && line.Length > 0)
                        {
                            string[] sCells = line.Split('\t');
                            for (int i = 0; i < sCells.GetLength(0); ++i)
                            {
                                if (iCol + i < this.dgData.ColumnCount)
                                {
                                    oCell = dgData[iCol + i, iRow];
                                    if (!oCell.ReadOnly)
                                    {
                                        if (oCell.Value==null|| oCell.Value.ToString() != sCells[i])
                                        {
                                            oCell.Value = Convert.ChangeType(sCells[i],
                                                                  oCell.ValueType);
                                          //  oCell.Style.BackColor = Color.Tomato;
                                        }
                                        else
                                            iFail++;
                                        //only traps a fail if the data has changed 
                                        //and you are pasting into a read only cell
                                    }
                                }
                                else
                                { break; }
                            }
                            iRow++;
                        }
                        else
                        { break; }
                        if (iFail > 0)
                            MessageBox.Show(string.Format("{0} updates failed due" +
                                            " to read only column setting", iFail));
                    }
                }
                catch (FormatException)
                {
                    MessageBox.Show("The data you pasted is in the wrong format for the cell");
                    return;
                }
            }
    

    【讨论】:

      【解决方案2】:

      是的,你可以!

      查看一些代码 here 看看是否有帮助。

      【讨论】:

        【解决方案3】:

        您可以将 excel 表读取到数据集,然后将数据集绑定到网格。我认为您应该能够进行修改,然后在完成编辑后再次写入 Excel。

        【讨论】:

          【解决方案4】:

          还有这个 MSDN 链接:how to PASTE (Ctrl+V, Shift+Ins) the data from clipboard to DataGridView

          带有 .NET C#、VB 和 C++ 的代码

          (在 GetData 方法中你可能要指定 Format.UnicodeText)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-07-29
            • 2010-09-24
            • 1970-01-01
            • 1970-01-01
            • 2013-12-06
            • 1970-01-01
            相关资源
            最近更新 更多