使用Microsoft.Office.Interop的VB .NET InvalisCastException

时间:2014-02-09 19:01:17

标签: .net vb.net excel

NET代码(Microsoft.Office.Interop已导入)。当我尝试调试时什么也没说,但是当我运行它时,打开2个文件(List.xls和File3.xlsm),当它到达如果sh1.Cells(x + 1,2)<> ; “然后说InvalisCastException。 什么deos意味着什么?我该如何修复代码?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim oXL As Excel.Application
    Dim Wk1, Wk2, Wk3 As Excel.Workbook
    Dim Sh1, Sh2, Sh3, Sh4 As Excel.Worksheet
    Dim nomefile As Object, seleziona As Object
    Dim x As Integer, y As Integer, v As Integer, u As Integer, w As Integer, t As Integer, z As Integer
    ' Start Excel and get Application object.
    oXL = CreateObject("Excel.Application")
    oXL.Visible = True
    'set and open first Workbook
    Wk1 = oXL.Workbooks.Open(Filename:="C:\xxx\List.xls")
    'set and open third Workbook
    Wk3 = oXL.Workbooks.Open(Filename:="C:\xxx\File3.xlsm")
    'set first sheet (list.xls)
    Sh1 = Wk1.Worksheets("List")
    'set third sheet (File3.xlsm)
    Sh3 = Wk3.Worksheets("All")

    For x = 1 To 1000
        **If sh1.Cells(x + 1, 2) <> "" Then**
            'select filename from sh1 (list.xls)
            seleziona = Sh1.Cells(x + 1, 2)
            nomefile = "C:\xxx\" & Left(seleziona, 1) & "\" & seleziona & ".xls"
            'set and open filename from list
            Wk2 = oXL.Workbooks.Open(Filename:=nomefile)
            'set second sheet (nomefile.xls)
            Sh2 = Wk2.Worksheets("ESAMI")
            'set forth sheet (nomefile.xls)
            Sh4 = Wk2.Worksheets("Foglio3")

            z = 0
            y = 0
            v = 1
            u = 1
            w = 0
            t = 0
            While z = 0
                If sh2.Cells(1, y + 4) <> "" Then
                    'find empty cells on sh2 first row
                    y = y + 1
                Else : sh2.Range(sh2.Cells(1, 4), sh2.Cells(114, (y + 4) - 1)).Copy()
                    'copy/paste from sh2 to sh4 of not-empty data (last not-empty row is always 114)
                    Sh4.Range("C1").PasteSpecial(Excel.XlPasteType.xlPasteValuesAndNumberFormats, SkipBlanks:=False, Transpose:=True)
                    'clear clipboard
                    Clipboard.Clear()
                    While w = 0
                        If Sh4.Cells(v, 3) <> "" Then
                            'set column 2 on sh4 equal to file's name
                            Sh4.Cells(v, 2) = seleziona
                            v = v + 1
                        Else : Sh4.Cells(v - 1, 1) = "u"
                            'find last non empty row on sh4 and put a "u" on first row
                            Sh4.Range(Sh4.Cells(1, 1), Sh4.Cells(v - 1, 116)).Copy()

                            While t = 0
                                If Sh3.Cells(u, 2) <> "" Then
                                    'copy all from sh4 (nomefile.xls) to sh3 (file3.xlsm)
                                    u = u + 1
                                Else
                                    Sh3.Range(Sh3.Cells(u, 1), Sh3.Cells(u, 1)).PasteSpecial(Excel.XlPasteType.xlPasteFormulasAndNumberFormats, SkipBlanks:=False, Transpose:=False)
                                    Clipboard.Clear()
                                    t = 1
                                End If
                            End While
                            w = 1
                        End If
                    End While
                    z = 1
                End If
            End While
            Clipboard.Clear()
            'close opened file from list
            Wk2.Close(False)

        End If

    Next
    wk2 = Nothing
    sh1 = Nothing
    sh2 = Nothing
    sh3 = Nothing
    Wk1.Close(False)





End Sub



End Class

2 个答案:

答案 0 :(得分:0)

您正在尝试将Range对象评估为字符串,这就是您收到错误的原因。这就像试图做的那样:

Dim x as Integer
x = 4
If x = "banana" Then ...

由于x是一个整数,因此无法将其评估为导致类似错误的字符串。

正如评论所建议的那样,请尝试使用If sh1.Cells(x + 1, 2).Value <> "" Then ...

答案 1 :(得分:0)

如果sh1.Cells(x + 1,2).Value&lt;&gt; “”那么

完成并正在工作,但现在我还有另一个问题

nomefile =“C:\ xxx \”&amp; 左(seleziona,1)&amp; “\”&amp; seleziona&amp;是 “.xls”

如果Left不起作用,我怎样才能获得变量seleziona的第一个字符?