![](/img/trans.png)
[英]Is it possible to paste Excel / CSV data from clipboard to DataGridView in C#?
[英]Paste to Excel From DataGridView
我想從 dataGridView 復制一個單元格區域,然后將該數據粘貼到 MS Excel 中。 我可以復制數據並粘貼到 MS Word 或記事本中,但不能粘貼到 excel 中。 有很多從 Excel 復制並粘貼到 DataGridView 的示例,但反之則不然。
private void frm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
this.dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
if (this.dataGridView1.GetCellCount(DataGridViewElementStates.Selected) > 0)
{
try
{
DataObject d = dataGridView1.GetClipboardContent();
Clipboard.SetDataObject(d);
}
catch (System.Runtime.InteropServices.ExternalException)
{
MessageBox.Show("Could Not Copy To Clipboard");
}
}
}
}
有什么方法可以粘貼到excel中嗎? 我一直在努力尋找解決方案。
編輯
在嘗試使用 dataGridViews 嘗試其他一些程序后出現,默認情況下,您可以在選擇 dataGridView 中的一組單元格后復制並粘貼到 Excel 或其他程序。 我現在無法確定是 DGV 中不受支持的數據還是我在屬性管理器中更改的 DGV 屬性,或者我只需要關閉並重新打開 excel 因為有一些錯誤。
我不能說這將如何與您的網格對齊,但這里有一個將格式化文本放在剪貼板上的示例。 這是為了粘貼到 Excel 中而編寫的:
StringBuilder sb = new StringBuilder();
for (Int32 r = Selection.r1; r <= Selection.r2; r++)
{
if (Rows[r].Visible)
{
if (!String.IsNullOrEmpty(sb.ToString()))
sb.Append(Environment.NewLine);
for (Int32 c = Selection.c1; c <= Selection.c2; c++)
{
if (!sb.ToString().EndsWith(Environment.NewLine) &&
!String.IsNullOrEmpty(sb.ToString()) &&
!sb.ToString().EndsWith("\t"))
sb.Append("\t");
if (String.IsNullOrEmpty(this[r, c] as String))
sb.Append(" ");
else
sb.Append(this[r, c].ToString());
}
}
}
if (sb.Length > 0)
ClipboardEx.SetTextThreadSafe(sb.ToString());
注意: Selection
是我的網格對突出顯示內容的引用, this
是獲取單元格中數據的單元格引用。
基本上,它試圖構建一個像這樣的文本塊:
R1C1\tR1C2\tR1C3\tR1C4\n
R2C1\tR2C2\tR2C3\tR2C4\n
R3C1\tR3C2\tR3C3\tR3C4\n
選項卡 (\\t) 告訴它向右移動一列,換行符 (\\n) 告訴它向下移動一行。 這是一個非常標准的格式。 如果你在那里復制並粘貼到你的網格中,Excel 會給你什么。
好吧,我最終在不斷尋找的過程中找到了答案。
如果我改變Clipboard.SetDataObject(d);
到Clipboard.SetData(DataFormats.Html, d);
這允許我將選定的單元格范圍粘貼到 excel。
public class MyDataGridView : DataGridView
{
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Insert:
case Keys.C:
return this.ProcessInsertKey(e.KeyData);
default:
break;
}
return base.ProcessDataGridViewKey(e);
}
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected new bool ProcessInsertKey(Keys keyData)
{
if ((((keyData & (Keys.Alt | Keys.Control | Keys.Shift)) == Keys.Control) ||
(((keyData & (Keys.Alt | Keys.Control | Keys.Shift)) == (Keys.Control | Keys.Shift))
&& ((keyData & Keys.KeyCode) == Keys.C)))
&& (this.ClipboardCopyMode != DataGridViewClipboardCopyMode.Disable))
{
DataObject clipboardContent = this.GetClipboardContent();
if (clipboardContent != null)
{
Clipboard.SetText(clipboardContent.GetData(DataFormats.UnicodeText).ToString());
return true;
}
}
return false;
}
}
您可以使用此自定義控件。
為什么不簡單地執行此操作 [無需檢查剪貼板,因為您實際上是在執行 CTRL-C ]
private void frm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
dataGridView1.SelectAll();
Clipboard.SetDataObject(dataGridView1.GetClipboardContent());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.