在 vb.net 中声明/打开 Excel 文件

发布于 2024-11-30 21:49:12 字数 2016 浏览 1 评论 0原文

我已经尝试在 vb.net 中声明或打开 Excel 工作表一段时间了。 我已经阅读了 vb.net 中的 Excel 文件 和其他链接,但它不起作用。

我添加了 Microsoft Excel 12.0 对象库。 我包括:

Imports Microsoft.VisualBasic
Imports System.Net.Mime.MediaTypeNames
Imports Microsoft.Office.Interop 

我想在模块中声明/打开 Excel 文件:

Public Module postleitzahlen_array

Dim myarray As String

Dim xlApp As Excel.Application
xlApp = New Excel.ApplicationClass ' here is the error, XlApp "has to be declared"

有人可以帮助我吗?

编辑:

好的,我注意到我使用 excel 2007,并且有一个区别 - 现在我使用 http://vb.net-informations.com/excel-2007/vb.net_excel_2007_create_file.htm

Sub test()
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet

        Dim misValue As Object = System.Reflection.Missing.Value

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        xlWorkSheet.Cells(1, 1) = "http://vb.net-informations.com"
        xlWorkSheet.SaveAs("D:\vbexcel.xlsx")

        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("Excel file created , you can find the file c:\")
    End Sub


    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub

但我在 xlWorkSheet = xlWorkBook.Sheets("sheet1") 说“(HRESULT异常:0x8002000B(DISP_E_BADINDEX))

Edit2: 我使用德语 excel,因此“sheet1”会引发错误 --> “tabelle1”是正确的词:)

I've been trying for a while to declare or to open a excel sheet in vb.net.
I already read excel file in vb.net
and other links but it doesn't work.

I added Microsoft Excel 12.0 Object Library.
I included:

Imports Microsoft.VisualBasic
Imports System.Net.Mime.MediaTypeNames
Imports Microsoft.Office.Interop 

I want to declare / open the excel file in a module:

Public Module postleitzahlen_array

Dim myarray As String

Dim xlApp As Excel.Application
xlApp = New Excel.ApplicationClass ' here is the error, XlApp "has to be declared"

Can someone help me?

EDIT:

Okay, i noticed that i use excel 2007, and there is a difference - now I'm using follwing code from http://vb.net-informations.com/excel-2007/vb.net_excel_2007_create_file.htm

Sub test()
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet

        Dim misValue As Object = System.Reflection.Missing.Value

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        xlWorkSheet.Cells(1, 1) = "http://vb.net-informations.com"
        xlWorkSheet.SaveAs("D:\vbexcel.xlsx")

        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("Excel file created , you can find the file c:\")
    End Sub


    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub

but I get an error in xlWorkSheet = xlWorkBook.Sheets("sheet1") saying "(Exception by HRESULT: 0x8002000B (DISP_E_BADINDEX))

Edit2:
I use a german excel, so "sheet1" throws an error --> "tabelle1" is the right word :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未央 2024-12-07 21:49:13

我编写了一个用于在 VB.NET 中获取工作簿的函数,该函数将尝试获取任何正在运行的 Excel 进程的句柄,并且仅在找不到时打开一个新的 Excel 实例。

    ''' <summary>
    ''' Open an Excel file and get a handle to the object.
    ''' If you want to quit Excel later use: `thisWorkbook.Application.Quit`
    ''' </summary>
    ''' <param name="fileName">full path to the workbook you want to open.</param>
    ''' <returns>The open Excel Workbook handle.</returns>
    Public Function OpenExcelFile(fileName As String) As Excel.Workbook
        Dim thisWorkbook As Excel.Workbook

        Dim excelApp As Excel.Application
#If DEBUG Then
        excelApp = New Excel.Application With {
            .Visible = True,
            .EnableEvents = False   ' Suppress the Workbook_Open event
            }
#Else
        Try ' To attach to running process
            excelApp = Marshal.GetActiveObject("Excel.Application")
        Catch ex As Exception
            ' Open Excel if it isn't already
            excelApp = New Excel.Application With {
                .Visible = False,       ' Ninja mode
                .EnableEvents = False,  ' Suppress the Workbook_Open event
                .DisplayAlerts = False  ' Supress messages
            }
        End Try
#End If

        Try
            Dim excelBooks As Excel.Workbooks = excelApp.Workbooks
            thisWorkbook = excelBooks.Open(fileName)
        Catch ex As Exception
            MsgBox("Error opening Excel file: " & fileName & vbCrLf & ex.Message)
        End Try

        Return thisWorkbook
    End Function

I wrote a function for getting a workbook in VB.NET that will attempt to get a handle on any running Excel process and only opens a new Excel instance when it can't find one.

    ''' <summary>
    ''' Open an Excel file and get a handle to the object.
    ''' If you want to quit Excel later use: `thisWorkbook.Application.Quit`
    ''' </summary>
    ''' <param name="fileName">full path to the workbook you want to open.</param>
    ''' <returns>The open Excel Workbook handle.</returns>
    Public Function OpenExcelFile(fileName As String) As Excel.Workbook
        Dim thisWorkbook As Excel.Workbook

        Dim excelApp As Excel.Application
#If DEBUG Then
        excelApp = New Excel.Application With {
            .Visible = True,
            .EnableEvents = False   ' Suppress the Workbook_Open event
            }
#Else
        Try ' To attach to running process
            excelApp = Marshal.GetActiveObject("Excel.Application")
        Catch ex As Exception
            ' Open Excel if it isn't already
            excelApp = New Excel.Application With {
                .Visible = False,       ' Ninja mode
                .EnableEvents = False,  ' Suppress the Workbook_Open event
                .DisplayAlerts = False  ' Supress messages
            }
        End Try
#End If

        Try
            Dim excelBooks As Excel.Workbooks = excelApp.Workbooks
            thisWorkbook = excelBooks.Open(fileName)
        Catch ex As Exception
            MsgBox("Error opening Excel file: " & fileName & vbCrLf & ex.Message)
        End Try

        Return thisWorkbook
    End Function
恍梦境° 2024-12-07 21:49:12

至于您的错误,将 ApplicationClass 替换为简单的 Application 已经解决了我的问题。

As for your error, substituting ApplicationClass to simply Application had solved my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文