Option Strict on时Excel列错误

时间:2017-01-11 11:53:01

标签: vb.net

以下代码效果很好。

Option Strict Off
Imports Microsoft.Office.Interop

Public Class Form1

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    Dim xlApp As Excel.Application = Nothing
    xlApp = CType(CreateObject("Excel.Application"), Excel.Application)

    xlApp.WindowState = Excel.XlWindowState.xlMinimized

    Dim wb1 As Excel.Workbook
    wb1 = xlApp.Workbooks.Open("C:\MyFolder\Book1.xlsx")

    With CType(wb1.Sheets(1), Excel.Worksheet)
        .Columns(4).Cut()
        .Columns(2).Insert()
    End With

End Sub

End Class

当我打开Option Strict时,会发生错误。 提前致谢。 祝你有愉快的一天。

1 个答案:

答案 0 :(得分:2)

由于您使用的是Office-Interop,因此所有对象都是Dim workSheet = CType(wb1.Sheets(1), Excel.Worksheet) Dim column4Range = CType(workSheet.Columns(4), Excel.Range) Dim column2Range = CType(workSheet.Columns(2), Excel.Range) column4Range.Cut() column2Range.Insert() 类型,因此您需要强制转换它们:

With

(你注意到我更喜欢 restoredCount ++ editor1.putInt("name", restoredCount); editor1.commit(); 语句中的变量)