vb.net 开发 excel Addin学习(1)---- 准备

  安装 vs.net2005 

  安装 microsoft visual studio 2005 tools for office runtime
                    microsoft visual studio 2005 tools for office runtime language pack

  安装office2003 sp2   +  .net 支持

  创建一个项目

  .net 中新建一个 其他项目类型 -〉 扩展性 -〉 共享的外接程序

  项目中引用

  extensibility

  Microsoft Office 11.0 Object Library

        COM:Microsoft Excel 11.0 Object Library

  程序中导入

  imports Extensibility
        Imports System.Runtime.InteropServices
        Imports Microsoft.Office.Interop

  excel对象模型介绍

  application--> workbook-->worksheet->cell

        例子代码:(只要注意其中的四行即可)

imports  Extensibility
Imports  System.Runtime.InteropServices
Imports  Microsoft.Office.Interop

Read me for Add-in installation and setup information.

< GuidAttribute( " 010A79D2-C4FB-4FF0-8580-2A842A4A8601 " ), ProgIdAttribute( " myExcel.Connect " ) >  _
Public   Class Connect
    
    
Implements Extensibility.IDTExtensibility2

    
Dim app As Excel.Application    '注意这行
        Dim addInInstance as object
    
    
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown

    
End Sub

    
    
Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate

    
End Sub

    
    
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete

    
End Sub

    
    
Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection

    
End Sub

    
    
Public Sub OnConnection(ByVal application As ObjectByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As ObjectByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
        app 
= CType(application, Excel.Application)
      
        addInInstance 
= addInInst
      
''请注意下面的代码
         Dim wb As Excel.Workbook
        If app.Workbooks.Count > 0 Then
            wb = app.ActiveWorkbook
        Else
            wb = app.Workbooks.Add
        End If
        Dim ws As Excel.Worksheet
        If wb.Worksheets.Count > 0 Then
            ws = wb.ActiveSheet
        Else
            ws = wb.Worksheets.Add
        End If

     ws.Cells(
86).value = "I am northsnow "

    
End Sub



End Class

  生成 解决方案,安装 

  启动excel 既看到效果

         

你可能感兴趣的:(vb.net 开发 excel Addin学习(1)---- 准备)