gpt4 book ai didi

vb.net - Excel 与 VB.NET(HRESULT : 0x8002000B (DISP_E_BADINDEX)) 异常

转载 作者:行者123 更新时间:2023-12-02 03:29:32 24 4
gpt4 key购买 nike

我第一次尝试从 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) = "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

错误代码在行中:

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

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

最佳答案

也许您的 Excel 版本不支持英语。 “sheet”在本地语言中是一个肮脏的词,它有点像英语;)你的名字暗示英语不是默认语言。使用索引而不是名称可以避免这样的意外:

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

关于vb.net - Excel 与 VB.NET(HRESULT : 0x8002000B (DISP_E_BADINDEX)) 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14665903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com