|
通过复制了该链接
https://docs.microsoft.com/zh-cn ... est-the-vsto-add-in
可以操作Excel工作表和显示窗体控件,不明觉厉。
类:AddInUtilities.cs中的代码
- using System.Data;
- using System.Runtime.InteropServices;
- using Excel = Microsoft.Office.Interop.Excel;
- using System.Windows.Forms;
- namespace ExcelImportData
- {
- [ComVisible(true)]
- public interface IAddInUtilities
- {
- void ImportData();
- void showform();
- }
- [ComVisible(true)]
- [ClassInterface(ClassInterfaceType.None)]
- public class AddInUtilities : IAddInUtilities
- {
- public void ImportData()
- {
- Excel.Worksheet activeWorksheet = Globals.ThisAddIn.Application.ActiveSheet as Excel.Worksheet;
- if (activeWorksheet != null)
- {
- Excel.Range range1 = activeWorksheet.get_Range("A1", System.Type.Missing);
- range1.Value2 = "This is my data";
- }
- }
- public void showform()
- {
- MessageBox.Show("显示窗体", "提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
- NewForm f = new NewForm();
- f.Show();
- }
- }
- }
复制代码 ThisAddIn.cs中的代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml.Linq;
- using Excel = Microsoft.Office.Interop.Excel;
- using Office = Microsoft.Office.Core;
- using Microsoft.Office.Tools.Excel;
- namespace ExcelImportData
- {
- public partial class ThisAddIn
- {
- private AddInUtilities utilities;
- protected override object RequestComAddInAutomationService()
- {
- if (utilities == null)
- utilities = new AddInUtilities();
- return utilities;
- }
- private void ThisAddIn_Startup(object sender, System.EventArgs e)
- {
- }
- private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
- {
- }
- #region VSTO 生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要修改
- /// 使用代码编辑器修改此方法的内容。
- /// </summary>
- private void InternalStartup()
- {
- this.Startup += new System.EventHandler(ThisAddIn_Startup);
- this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
- }
-
- #endregion
- }
- }
复制代码 VBA中两个调用代码:
- Private Sub CommandButton1_Click()
- Dim addIn As COMAddIn
- Dim automationObject As Object
- Set addIn = Application.COMAddIns("ExcelImportData")
- Set automationObject = addIn.Object
- automationObject.ImportData
- End Sub
- Private Sub CommandButton2_Click()
- Dim addIn As COMAddIn
- Dim automationObject As Object
- Set addIn = Application.COMAddIns("ExcelImportData")
- Set automationObject = addIn.Object
- automationObject.showform
- End Sub
复制代码
|
-
VBA调用C#类方法
|