由类限制属性/方法创建的VB.Net 2013 Excel工作簿对象

时间:2015-08-30 04:38:46

标签: vb.net excel

我的代码的意图是使用类cWB打开工作簿并返回一个全功能的Workbook对象。 我的问题是我的类创建并返回的oBook对象,并没有让我访问 Excel.Workbook的完整属性/方法集。 例如,使用oBook.Sheets提供Intellisense消息:'错误:'表格'不是cWB的成员 任何建议赞赏。

Imports Excel = Microsoft.Office.Interop.Excel
Public Class cWB
    Public Property IsOpen As Boolean
    Public Property oBook As Excel.Workbook

    Sub New(ByVal xlApp As Excel.Application, ByVal filename As String)

        Try
            Me.oBook = xlApp.Workbooks.Open(Filename:=filename, ReadOnly:=True)
            Me.IsOpen = True
            Me.oBook = oBook
            Exit Sub

        Catch ex As Exception
            Me.IsOpen = False
            Me.oBook = Nothing
        End Try

    End Sub

    Sub Close()
        Me.oBook.Close()
        Me.oBook = Nothing
    End Sub
Imports Excel = Microsoft.Office.Interop.Excel

模块模块1

    Sub Main()
        Dim xlApp As New Excel.Application
        Dim filename As String = "D:\workbook.xls"
        Dim oBook As New cWB(xlApp, filename)

        Try
            If oBook.IsOpen = True Then
                Console.WriteLine("oBook.oBook=" & oBook.oBook.Name _
                                  & " now open=" & oBook.IsOpen)

                'Error: 'Sheets' is not a member of cWB
                'Console.WriteLine(oBook.Sheets(1).Cells(1, 1).Value())

            Else
                Console.WriteLine("oBook.oBook=" & oBook.oBook.Name _
                                  & " now open=" & oBook.IsOpen)
            End If

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

        Console.WriteLine("Cleaning up")
        oBook.Close()
        Console.ReadLine()

        xlApp.Quit()
        releaseObject(xlApp)
        releaseObject(oBook)
    End Sub

输出:

oBook.oBook=workbook.xls now open=True
Cleaning up  

0 个答案:

没有答案