vb.net开发中使用excel插件代码
2010-02-25 23:14
447 查看
Public Class ConnectClass Connect
Implements Extensibility.IDTExtensibility2
Dim app As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim addInInstance As Object
Dim pasteText As Microsoft.Office.Core.CommandBarButton ‘定义一个工具栏按钮
Public Sub OnBeginShutdown()Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub OnAddInsUpdate()Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete()Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnDisconnection()Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnDisconnection
End Sub
Public Sub OnConnection()Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode,
ByVal addInInst As Object, ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnConnection
If TypeOf (application) Is Excel.Application Then app = CType(application, Excel.Application)
addInInstance = addInInst
wb = app.ActiveWorkbook
ws = CType(wb.ActiveSheet, Excel.Worksheet)
Dim toolbar As Microsoft.Office.Core.CommandBar = Nothing
If Not app Is Nothing Then
toolbar = AddToolbar(app, "专用工具栏") ’添加工具栏
End If
’ 建立按钮添加文本
pasteText = MakeANewButton(toolbar, "Insert text",
1044, AddressOf pasteText_Click) ’添加工具栏按钮并帮定事件
End Sub
’添加工具栏
Private Function AddToolbar()Function AddToolbar(ByVal ex As Excel.Application, _
ByVal toolbarName As String) As Microsoft.Office.Core.CommandBar
Dim toolBar As Microsoft.Office.Core.CommandBar = Nothing
Try
’ 为add-in建立一个命令条
toolBar = CType(ex.CommandBars.Add(toolbarName,
Microsoft.Office.Core.MsoBarPosition.msoBarTop, , True),
Microsoft.Office.Core.CommandBar)
toolBar.Visible = True
Return toolBar
Catch
Return Nothing
End Try
End Function
’在工具栏上添加一个按钮
Private Function MakeANewButton()Function MakeANewButton(ByVal commandBar As Microsoft.Office.Core.CommandBar, ByVal caption
As String, ByVal faceID As Integer, ByVal clickHandler As Microsoft.Office.
Core._CommandBarButtonEvents_ClickEventHandler) As Microsoft.Office.Core.CommandBarButton
Try
Dim newButton As Microsoft.Office.Core.CommandBarButton
newButton = CType(commandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton)
newButton.Caption = caption
newButton.FaceId = faceID
AddHandler newButton.Click, clickHandler
Return newButton
Catch ex As System.Exception
Return Nothing
End Try
End Function
’点击工具条按钮时应该执行的程序
Public Sub pasteText_Click()Sub pasteText_Click(ByVal barButton As Microsoft.Office.Core.CommandBarButton, ByRef someBool As Boolean)
Dim text As String = ""
Dim data As System.Windows.Forms.IDataObject = System.Windows.Forms.Clipboard.GetDataObject()
If data.GetDataPresent(System.Windows.Forms.DataFormats.Text) Then
text = data.GetData(System.Windows.Forms.DataFormats.Text).ToString()
If (Not app Is Nothing) Then
Me.app.ActiveCell.Value = text
End If
End If
End Sub
End Class
Implements Extensibility.IDTExtensibility2
Dim app As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim addInInstance As Object
Dim pasteText As Microsoft.Office.Core.CommandBarButton ‘定义一个工具栏按钮
Public Sub OnBeginShutdown()Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub OnAddInsUpdate()Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete()Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnDisconnection()Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnDisconnection
End Sub
Public Sub OnConnection()Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode,
ByVal addInInst As Object, ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnConnection
If TypeOf (application) Is Excel.Application Then app = CType(application, Excel.Application)
addInInstance = addInInst
wb = app.ActiveWorkbook
ws = CType(wb.ActiveSheet, Excel.Worksheet)
Dim toolbar As Microsoft.Office.Core.CommandBar = Nothing
If Not app Is Nothing Then
toolbar = AddToolbar(app, "专用工具栏") ’添加工具栏
End If
’ 建立按钮添加文本
pasteText = MakeANewButton(toolbar, "Insert text",
1044, AddressOf pasteText_Click) ’添加工具栏按钮并帮定事件
End Sub
’添加工具栏
Private Function AddToolbar()Function AddToolbar(ByVal ex As Excel.Application, _
ByVal toolbarName As String) As Microsoft.Office.Core.CommandBar
Dim toolBar As Microsoft.Office.Core.CommandBar = Nothing
Try
’ 为add-in建立一个命令条
toolBar = CType(ex.CommandBars.Add(toolbarName,
Microsoft.Office.Core.MsoBarPosition.msoBarTop, , True),
Microsoft.Office.Core.CommandBar)
toolBar.Visible = True
Return toolBar
Catch
Return Nothing
End Try
End Function
’在工具栏上添加一个按钮
Private Function MakeANewButton()Function MakeANewButton(ByVal commandBar As Microsoft.Office.Core.CommandBar, ByVal caption
As String, ByVal faceID As Integer, ByVal clickHandler As Microsoft.Office.
Core._CommandBarButtonEvents_ClickEventHandler) As Microsoft.Office.Core.CommandBarButton
Try
Dim newButton As Microsoft.Office.Core.CommandBarButton
newButton = CType(commandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton)
newButton.Caption = caption
newButton.FaceId = faceID
AddHandler newButton.Click, clickHandler
Return newButton
Catch ex As System.Exception
Return Nothing
End Try
End Function
’点击工具条按钮时应该执行的程序
Public Sub pasteText_Click()Sub pasteText_Click(ByVal barButton As Microsoft.Office.Core.CommandBarButton, ByRef someBool As Boolean)
Dim text As String = ""
Dim data As System.Windows.Forms.IDataObject = System.Windows.Forms.Clipboard.GetDataObject()
If data.GetDataPresent(System.Windows.Forms.DataFormats.Text) Then
text = data.GetData(System.Windows.Forms.DataFormats.Text).ToString()
If (Not app Is Nothing) Then
Me.app.ActiveCell.Value = text
End If
End If
End Sub
End Class
相关文章推荐
- vb.net开发中使用excel插件学习
- 使用VB.NET加快代码开发速度
- 浅谈vb.net开发中使用excel插件代码
- 使用VB.net Express 2010开发AutoCAD.net插件调试时出现很多错误的解决办法
- 开发人员在使用Aspose.Pdf for .NET时如何用C#和VB两种代码添加附件到PDF文档?
- VisualStudio移动开发(C#、VB.NET)Smobiler开发平台——ImageTabBar控件的使用方式
- 使用VB.NET开发纯.NET的应用程序
- [转]多操作系统平台协同开发时 GIT 的注意事项: 不同操作系统中的换行符(不要使用git的换行符自动转换功能,并且,尽可能保证代码的换行符都是unix); 避免使用第三方插件(如 EGit );
- 在Asp.net中使用JQuery插件之jTip代码
- 使用vb.net 语言将sqlserver数据库中的数据显示到bomboBox中的代码
- 使用VB.NET开发多线程
- C#开发中使用Npoi操作excel实例代码
- 使用VB.NET开发复合控件
- vb.net中导出至excel代码
- VB.Net导出SQLServer数据到Excel中代码架构段
- C#开发中使用Npoi操作excel实例代码
- 用Visual Studio 2005 开发VB.NET-C#语言转换插件
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- 使用asp.net开发客户端脚本代码(Chinese ,from csdn)