在 vb.net 中声明/打开 Excel 文件
我已经尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我编写了一个用于在 VB.NET 中获取工作簿的函数,该函数将尝试获取任何正在运行的 Excel 进程的句柄,并且仅在找不到时打开一个新的 Excel 实例。
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.
至于您的错误,将
ApplicationClass
替换为简单的Application
已经解决了我的问题。As for your error, substituting
ApplicationClass
to simplyApplication
had solved my problem.