Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 即使在设置变量后,vb.net后期绑定也会出现问题_Excel_Vb.net_Binding_Office Interop_Late Binding - Fatal编程技术网

Excel 即使在设置变量后,vb.net后期绑定也会出现问题

Excel 即使在设置变量后,vb.net后期绑定也会出现问题,excel,vb.net,binding,office-interop,late-binding,Excel,Vb.net,Binding,Office Interop,Late Binding,嘿,我有下面的代码,它抛出了后期绑定。 我想: Dim excelWS As Worksheet excelWS = New Worksheet 在使用变量之前,是否更正了后期绑定问题 更新1 是这样的吗 Dim excelRange As Range Dim excelApp As Application Dim excelWB As Workbook Dim excelWS As Worksheets excelWB = New Workbook If madeSheet = Fa

嘿,我有下面的代码,它抛出了后期绑定。

我想:

Dim excelWS As Worksheet

excelWS = New Worksheet
在使用变量之前,是否更正了后期绑定问题

更新1

是这样的吗

Dim excelRange As Range
Dim excelApp As Application
Dim excelWB As Workbook
Dim excelWS As Worksheets

excelWB = New Workbook

If madeSheet = False Then
   excelApp = New Application
   excelWB = excelApp.Workbooks.Add
   excelApp.Visible = True
End If

excelWS = New Worksheet
更新2

现在在这条线上:

excelWS = excelWB.Worksheets.Add(After:=excelWB.Worksheets(sheetLoops))
我得到的错误是:

其他信息:无法将“System.\u ComObject”类型的COM对象强制转换为接口类型“Microsoft.Office.Interop.Excel.Worksheets”。此操作失败,因为对IID为“{000208B1-0000-0000-C000-0000000000 46}”的接口的COM组件的QueryInterface调用由于以下错误而失败:不支持来自HRESULT的此类接口异常:0x80004002 E_NOINTERFACE


请注意,您必须引用Microsoft.Office.Interop.Excel程序集: 项目>>添加参考>>检查Microsoft Excel x.xx对象库

Imports Microsoft.Office.Interop
Public Class Form1
    Private exapp As Excel.Application
    Private xlwb As Excel.Workbook
    Private xlws As Excel.Worksheet
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        exapp = New Excel.Application

        xlwb = exapp.Workbooks.Add()
        xlws = xlwb.Worksheets.Add()
        xlws.Name = "MY WS"


        xlws.Move(After:=xlwb.Sheets(xlwb.Sheets.Count))
        ' note: .value is a Range property
        xlws.Cells(1, 2) = "standard"


        xlwb.Application.DisplayAlerts = False
        exapp.Visible = True
        xlwb.Worksheets("sheet1").Delete()
        xlwb.SaveAs("C:\Users\john\Desktop\test.xlsx")
        xlwb.Close()

    End Sub
End Class
至于你的更新:

即使是Excel.Worksheet和Excel.Worksheet也不是静态对象,在您的情况下,您不需要立即使用new,因为您正在使用新工作簿和新工作表初始化它们

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim excelRange As Excel.Range
        Dim excelApp As Excel.Application
        Dim excelWB As Excel.Workbook
        Dim excelWS As Excel.Worksheets

        ' excelWB = New Workbook - you dont need an instant

        ' If madeSheet = False Then
        excelApp = New Excel.Application

        excelWB = excelApp.Workbooks.Add
            excelApp.Visible = True
        '  End If

        ' excelWS = New Worksheet - you dont need an instant
    End Sub

它是vb.net。你为什么不引用导入“Microsoft.Office.Interop.Excel”并使用“CreateObject”?@jonathana介意给出一个代码示例吗?没问题,可以将代码添加为文本而不是图像吗?@jonathana请检查我更新的OP。投票被否决的人请解释原因。你是想使用Excel吗。所有这些?您不能仅使用应用程序、工作簿或工作表吗?是的,您可以将Imports Microsoft.Office.Interop更改为Imports Microsoft.Office.Interop.Excels查看我的操作进行第二次更新,查看我现在在按照您的建议更改所有内容时得到的结果。我认为您正在将索引为0的工作表移动到最左侧/最后一个工作表,并且无法将其移动到索引-1。我说的对吗?它处于循环中,将创建几个选项卡。我把所有的excel调用都设置为nothing,我想这样做会使它成为一个新的调用?