Excel-DNA项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【C#版】

Excel-DNA项目中的自定义功能区和自定义任务窗格需要用到各种命名空间、添加所需文件,才能实现。后来我发现可以把所有代码都写在Class1.cs这个默认文件中。

大家可以在Visual Studio中创建一个类库项目(.Net Framework),然后把默认的Class1.cs中的代码整体替换为下面我贴的这个代码。然后启动调试,就可以看到自定义功能区和任务窗格了。

 1 using System.Runtime.InteropServices;
 2 using ExcelDna.Integration;
 3 using ExcelDna.Integration.CustomUI;
 4 using Excel = Microsoft.Office.Interop.Excel;
 5 using System.Windows.Forms;
 6 namespace Excel_DNA_Template_CS
 7 {
 8     [ComVisible(true)]
 9     public class Class1 :ExcelRibbon,IExcelAddIn
10     {
11         public IRibbonUI R;
12         public override string GetCustomUI(string RibbonID)
13         {
14             string xml = @"<customUI xmlns=‘http://schemas.microsoft.com/office/2009/07/customui‘ onLoad=‘OnLoad‘>
15     <ribbon startFromScratch=‘false‘>
16         <tabs>
17             <tab id=‘Tab1‘ label=‘RibbonXmlEditor‘>
18                 <group id=‘Group1‘ label=‘Author:ryueifu‘>
19                     <button id=‘Button1‘ label=‘CTP‘ imageMso=‘C‘ onAction=‘Button1_Click‘/>
20                     <button id=‘Button2‘ label=‘UnLoad‘ imageMso=‘U‘ onAction=‘Button2_Click‘/>
21                 </group>
22             </tab>
23         </tabs>
24     </ribbon>
25 </customUI>";
26             return xml;
27         }
28         public void OnLoad(IRibbonUI ribbon)
29         {
30             R = ribbon;
31             R.ActivateTab(ControlID: "Tab1");
32         }
33         public void Button1_Click(IRibbonControl control)
34         {
35             Module1.ctp.Visible = !Module1.ctp.Visible;
36         }
37         public void Button2_Click(IRibbonControl control)
38         {
39             Excel.AddIn ThisAddin = (ExcelDnaUtil.Application as Excel.Application).AddIns["Excel_DNA_Template_CS"];
40             ThisAddin.Installed= false;
41         }
42         void IExcelAddIn.AutoClose()
43         {
44             Module1.DisposeCTP();
45         }
46
47         void IExcelAddIn.AutoOpen()
48         {
49             Module1.CreateCTP();
50         }
51     }
52     public static class Module1
53     {
54         public static UserControl uc;
55         public static CustomTaskPane ctp;
56         public static void CreateCTP()
57         {
58             uc = new UserControl();
59             ctp = CustomTaskPaneFactory.CreateCustomTaskPane(userControl: uc, title: "CTP");
60             ctp.DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight;
61             ctp.Visible = true;
62         }
63         public static void DisposeCTP()
64         {
65             ctp.Delete();
66             ctp = null;
67         }
68     }
69 }

原文地址:https://www.cnblogs.com/ryueifu-VBA/p/10122618.html

时间: 2024-08-01 04:45:17

Excel-DNA项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【C#版】的相关文章

VSTO外接程序项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【VB.Net版】

VSTO中的自定义功能区和自定义任务窗格需要用到各种命名空间.添加所需文件,才能实现.后来我发现可以把所有代码都写在ThisAddin.vb这个默认文件中. 大家可以在Visual Studio中创建一个外接程序项目,然后把ThisAddin.vb中的代码整体替换为下面我贴的这个代码.然后启动调试,就可以看到自定义功能区和任务窗格了. 1 Imports Microsoft.Office.Core 2 Public Class ThisAddIn 3 4 Private Sub ThisAddI

Excel DNA学习笔记(二)简单的UDF自定义函数

如何通过使用Excel DNA给Excel添加一个自定义的函数? 1.在类库项目上新建一个类文件,我们姑且叫他MyUdf.cs 2.在这个类中添加一个静态的方法,具体写法如下:注意:一定要是静态的方法 public class MyUdf { [ExcelFunction(Name = "Say", Description = "My first .NET function", Category = "这是类别分组")] public stati

Share data between VSTO and Excel DNA App domains

Is there a way to share data between a VSTO add in and an Excel DNA add in? Or can the Excel DNA add in be loaded into the VSTO's app domain? The Excel-DNA add-in will always be in a separate AppDomain. You might try to pass an object via the AddIn's

maven新建Spring MVC + MyBatis + Oracle的Web项目中pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion&

Eclipse无法打开项目中的任何文件

今天,Eclipse居然打不开项目的任何一个文件,网上也没搜到,我决定重置下视图试试: Windows-Reset Persective-然后能正常打开项目的文件了,解决问题!

iOS之多控制器管理--项目中的常见文件

*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } a { color: #4183C4; } a.absent { color: #cc0000; } a.anchor { display: block; padding-left: 30px; margin-left: -30px; cursor: pointer; position: absolute

项目--简单导出CSV文件

//导出 protected void BtnOutPut_Click(object sender, EventArgs e) { //角色 InitialRoles(); DataTable dt = DBClass.GetDataTable(string.Format(@"SELECT * FROM {0} Order By SN desc", View(Where))); StringWriter sw = new StringWriter(); //4S店不能看到总部价格等相关

php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页面存储到本地

php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页面存储到本地 yeah,搞定啦 php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页面存储到本地

C#项目,在controller文件夹右击鼠标没有“添加控制器”的问题

1.发现拷贝的项目,在controller文件夹右击鼠标不能没有“添加控制器”这一项,在控制器里面右键控制器名称也没有“添加视图”,这样就控制器和视图都要手写,开发效率低 . 2.原因是mvc包的问题,解决方法为:先卸载mvc包,再安装一次,再重启vs. 具体步骤: 1)在自己的web项目的“引用”上操作,如下图的“引用”,右击,点击”管理NuGet程序包“. 2)在弹出的如下界面,找到该项,点击“卸载”. 3)卸载完之后就安装这个. 4)一般的情况下,以上的步骤就可以完成了,重启一下项目后,问