![](/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.