c# - Winform拖放到excel

标签 c# excel winforms datagridview drag-and-drop

我想从我的 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/

相关文章:

c# - 翻转/镜像图像

excel - 如何在Excel中的列中应用分组值

c# - 在组合框上绘制动画

excel - 如何通过变量引用Excel工作表

c# - 应用程序配置文件的快速而肮脏的用户界面?

.net - 使用 Windows 窗体作为抽象类 - 使用哪种模式?

C#MVC : Return ViewModel or Model Class?

c# - 如何在Windows窗体上有效地多线程?

c# - 使 gridview 数据源保留在界面中而不是向下转换为对象

excel - 获取youtube视频的成绩单