带VB.NET的Excel(HRESULT异常:0x8002000B(DISP_E_BADINDEX)) [英] Excel with VB.NET (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

查看:2003
本文介绍了带VB.NET的Excel(HRESULT异常:0x8002000B(DISP_E_BADINDEX))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的第一次从VB.net创建一个Excel文件。
我已经添加了Microsoft.Office.Excel引用,导入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)=这里的东西
xlWorkSheet.SaveAs(D:\vbexcel.xlsx )

xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)

MsgBox(创建Excel文件,可以找到文件c:\)

End Sub

错误generetad在行中:

  xlWorkSheet = CType(xlWorkBook.Sheets(sheet1 ),Excel.Worksheet)

索引无效。 (HRESULT的异常:0x8002000B(DISP_E_BADINDEX))


解决方案

您的Excel版本不会说英文。而表格是当地语言中的肮脏的单词,它是英文的;)您的姓名是一个提示,英语不是默认语言。使用索引而不是名称来避免这样的事故:

  xlWorkSheet = CType(xlWorkBook.Sheets(1),Excel。工作表)


I'm trying to create an Excel file from VB.net from my first time. I already added the Microsoft.Office.Excel reference, Import the 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

The error generetad is in the line :

   xlWorkSheet = CType(xlWorkBook.Sheets("sheet1"), Excel.Worksheet)

   Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

解决方案

Maybe your version of Excel doesn't speak English. And "sheet" is a dirty word in the local language, it kinda is in English ;) Your name is a hint that English is not the default language. Use the index instead of the name to avoid accidents like this:

    xlWorkSheet = CType(xlWorkBook.Sheets(1), Excel.Worksheet)

这篇关于带VB.NET的Excel(HRESULT异常:0x8002000B(DISP_E_BADINDEX))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆