“使用VB.Net在Spreadsheetgear中打开现有文件

时间:2014-10-28 05:26:11

标签: vb.net excel spreadsheetgear

我正在尝试打开放在Windows窗体上的WorkBookView(在下面给出的代码中命名为wkbMain)中的现有文件。我使用以下代码:

Private Sub MenuItemOpen_Click(sender As Object, e As EventArgs)
    Dim lObjDialog As New OpenFileDialog

    wkbMain.GetLock()

    Try
        If lObjDialog.ShowDialog() = DialogResult.OK Then
            wkbMain = SpreadsheetGear.Factory.GetWorkbook(lObjDialog.FileName, System.Globalization.CultureInfo.CurrentCulture)
        End If
    Catch ex As Exception

    Finally
        wkbMain.ReleaseLock()
    End Try
End Sub

但是作业

wkbMain = SpreadsheetGear.Factory.GetWorkbook(lObjDialog.FileName, System.Globalization.CultureInfo.CurrentCulture)`) 

抛出异常:

  

无法投射类型'ᢷ'输入' SpreadsheetGear.Windows.Forms.WorkbookView'。

请建议解决方案

1 个答案:

答案 0 :(得分:1)

您需要将WorkbookView。ActiveWorkbook属性设置为Factory.GetWorkbook(...)返回的对象,而不是WorkbookView对象本身。例如:

wkbMain.ActiveWorkbook = SpreadsheetGear.Factory.GetWorkbook(lObjDialog.FileName, 
    System.Globalization.CultureInfo.CurrentCulture))