【问题标题】:declare/open excel file in vb.net在 vb.net 中声明/打开 excel 文件
【发布时间】:2011-08-24 09:26:08
【问题描述】:

我一直在尝试在 vb.net 中声明或打开一个 excel 表。 我已经读过excel file in vb.net 和其他链接,但它不起作用。

我添加了 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))

编辑2: 我使用的是德语 excel,所以“sheet1”会抛出错误 -->“tabelle1”是正确的词 :)

【问题讨论】:

    标签: vb.net excel file-io


    【解决方案1】:

    This 帮助我开始了,虽然对于 C#。我怀疑 VB 的概念是相似的。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • @Tyzak 这能回答你的问题吗?
      • 嗨,很抱歉我迟到的答案 - 它没有解决问题:-/
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多