我想从我的 winform 拖放到 Excel 中。例如,在 Winform 中按住一个按钮(或 DataGridView 中的一行)并将其拖放到 Excel 单元格中将执行一些操作或将一些数据写入 Excel 单元格。
我说的是 winform 和 Excel 之间的拖放,而不是 winform 组件之间的拖放。
我记得曾经看过一个示例,但在 Google 上找不到了。
更新:根据 Avantol13 的回答,
我们如何提取发送到 Excel 的对象?在示例中,将字符串写入单元格,然后检查该单元格的内容以执行进一步的操作,这是一种黑客攻击。也许类似于 DragDrop 事件,我们可以在其中提取从 e.Data.GetData(e.Data.GetFormats()[0]) 发送的数据
此外,使用 Application_SheetChange
知道 drop 事件将导致它循环为 Application_SheetChange
-> 修改单元格的代码,如示例(删除单元格和写点新东西)-> 再次触发Application_SheetChange
等
提前致谢。
最佳答案
Sean Sexton provides an excellent example关于如何将数据从 WPF 窗口拖放到 Excel。我修改了他的示例,以便它可以与 WinForms 一起使用。 试试这个:
private void control_MouseDown(object sender, MouseEventArgs e)
{
List<string> stringItems = new List<string>() { "Value1", "Value2", "Value3" };
//Separate all values with tabs
string someValues = string.Join("\t", stringItems);
DataObject data = new DataObject(DataFormats.Text, someValues);
DoDragDrop(data, DragDropEffects.Copy);
}
关于c# - Winform拖放到excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30329923/