【发布时间】: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”是正确的词 :)
【问题讨论】: