Discuz!NT
欢迎 游客 , 注册 | 登录 | 会员 | 界面 | 简洁版本 | 在线 | 帮助
商都网教育宝典宝库

发表新主题 回复该主题
本主题被查看603次, 共1个帖子, 1页, 当前为第1页     选择页数: 1      跳转到第   上一主题   下一主题
标题: vb.net开发中使用excel插件的代码
-[尕硴]
超级版主
UID: 71
来自:
精华: 130
积分: 14003
帖子: 12909
注册: 2007-10-22 17:59:00
状态: 离线
威望: 444.00
金钱: 3355.00 元
只看楼主 2008-02-18 14:09
vb.net开发中使用excel插件的代码
用VB.NET做excel插件开发,可能需要为excel添加自定义的工具栏。这个也是非常的简单的。

以下是引用片段:
  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

  这样程序就完成了,赶紧动手体验吧!
#1  
发表新主题 回复该主题
本主题被查看603次, 共1个帖子, 1页, 当前为第1页     选择页数: 1      跳转到第







现在的时间是 2008-08-30 21:07:32

版权所有 商都网教育宝典
         Powered by Discuz!NT 1.0.6666    Copyright © 2001-2008 Comsenz Inc.
Processed in 0.048 seconds