J'essaie de créer un fichier Excel à partir de VB.net pour la première fois. J'ai déjà ajouté la référence Microsoft.Office.Excel, importé la référence Microsoft.Office.Interop
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
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 = CType(xlWorkBook.Sheets("sheet1"), Excel.Worksheet)
xlWorkSheet.Cells(1, 1) = "Something here"
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
L'erreur générée se trouve dans la ligne :
xlWorkSheet = CType(xlWorkBook.Sheets("sheet1"), Excel.Worksheet)
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))