簡體   English   中英

是否可以將Excel / CSV數據從剪貼板粘貼到C#中的DataGridView?

[英]Is it possible to paste Excel / CSV data from clipboard to DataGridView in C#?

我使用DataGridView控件來管理一個簡單的字典(幾列和幾百行)。 DataGridView功能幾乎足夠。 我可以添加新行,修改值並將數據從其中復制到Excel。 我不能做的一件事是將數據從Excel復制到控件中。 有一些屬性可能嗎? 還是需要一些代碼來做到這一點?

這已從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;
            }
        }

是的你可以!

這里看看一些代碼,看看是否有幫助。

您可以將Excel工作表讀取到數據集,然后將數據集綁定到網格。 我認為您應該能夠進行修改,然后在完成編輯后再次寫出Excel。

也是這個MSDN鏈接: 如何將數據從剪貼板粘貼(Ctrl + V,Shift + Ins)到DataGridView

使用.NET C#,VB和C ++的代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM