我一直在尝试一段时间来声明或在vb.net中打开excel表单。 我已经阅读excel file in vb.net 和其他链接,但它不起作用。在vb.net中声明/打开excel文件
我添加了Microsoft Excel 12.0 Object Library。 我包括:
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"
有人能帮助我吗?
编辑:
好吧,我注意到,我使用的2007年Excel和是有区别的 - 现在我使用的follwing代码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中,所以 “Sheet 1中” 引发错误 - > “tabelle1” 是正确的字:)
@Tyzak这是否回答你的问题? –
嗨,对不起,我迟到的答案 - 它没有解决问题: -/ – Tyzak
但我编辑我的问题,因为我得到了进一步 – Tyzak