我一直在嘗試一段時間來聲明或在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