简体   繁体   English

如何从代码后面单击Excel功能区按钮

[英]How to click a Excel ribbon button from code behind

We use a 3rd party Excel Addin to populate data in an Excel worksheet. 我们使用第三方Excel Addin在Excel工作表中填充数据。 I've been given the task of writing a C# app (WinForms or VSTO) that will automatically click the Ribbon button and process the data. 我被赋予了编写C#应用程序(WinForms或VSTO)的任务,该应用程序将自动单击功能区按钮并处理数据。

The problem is I can't find any way to automate clicking the Ribbon button (on the Add-ins tab) using C# code. 问题是我找不到任何方法来自动使用C#代码单击功能区按钮(在加载项选项卡上)。

I've also tried using Win32 API's to send key presses to the parent Excel window (using SendMessage), but the Key presses weren't received. 我也尝试使用Win32 API将按键发送到父Excel窗口(使用SendMessage),但未收到按键。 (I checked using Spy++). (我使用Spy ++检查过)。 And looked at the public methods exposed in the Add-in assembly, but it didn't contain any public methods to do the same as clicking the ribbon button. 并查看了外接程序集中公开的公共方法,但它没有包含任何公共方法来执行与单击功能区按钮相同的操作。

Does anyone know how I can click a ribbon button in the Excel (add-ins) ribbon using C# code? 有谁知道如何使用C#代码单击Excel(加载项)功能区中的功能区按钮?

Look at the SendKeys method of the Excel Application class. 查看Excel Application类的SendKeys方法。 It allows you to send the keys directly to Excel. 它允许您将密钥直接发送到Excel。 You can press the Alt key to see which shortcuts are assigned to which buttons / ribbon tabs. 您可以按Alt键查看哪些快捷方式分配给哪些按钮/功能区选项卡。 I automated some tests using this and it worked fine. 我用这个自动化了一些测试,它运行良好。

If you needed even more complex things you could also look into Sikuli . 如果你需要更复杂的东西,你也可以看看Sikuli It's based on image recognition and is using Java.Robots class for sending keypresses and clicks. 它基于图像识别,并使用Java.Robots类发送按键和点击。 Automating UI tasks is quite easy with it. 使用它可以轻松实现UI任务的自动化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM