从 vb.net 打开 Excel 工作簿 Interop 打开时没有功能区或菜单
Opening Excel workbook from vb.net Interop opens without ribbon or menus
无法确定我的引用是否有误,或者是什么,但是当我通过 vb.net 打开工作簿时,它打开时没有可见的菜单或功能区,您甚至无法访问它们。它看起来像是 Excel 的某种安全版本。如何让它正常打开?
这是我的完整块:(相关导入是 Microsoft.Office.Interop.Excel
Private Sub ButtonExcel_Click(sender As Object, e As EventArgs) Handles ButtonExcel.Click
Cursor = Cursors.WaitCursor
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
Dim oExcel As Excel.Application = Nothing
Dim oBook As Excel.Workbook = Nothing
Dim oRosterSheet As Excel.Worksheet = Nothing
oExcel = New Excel.Application
oExcel.Visible = True
oBook = oExcel.Workbooks.Open("C:\****.xlsb", [ReadOnly]:=True)
oRosterSheet = oBook.Worksheets(1)
oRosterSheet.Cells.Clear()
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In RosterTable.Columns
colIndex += 1
oRosterSheet.Cells(1, colIndex) = dc.ColumnName
Next dc
For Each dr In RosterTable.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In RosterTable.Columns
colIndex = colIndex + 1
oRosterSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next dc
Next dr
Dim fileName As String = "C:\****.xlsb"
oRosterSheet.Columns.AutoFit()
oBook.SaveAs(Filename:=fileName)
oExcel.UserControl = True
ReleaseObject(oRosterSheet)
ReleaseObject(oBook)
ReleaseObject(oExcel)
GC.Collect()
Cursor = Cursors.Default
End Sub
想通了。原来它是在 "Full Screen" 模式下打开 Excel,但不知何故没有最大化。可能与我正在远程访问的计算机上编码有关。无论哪种方式,下面的行修复了它:
oExcel.DisplayFullScreen = False
无法确定我的引用是否有误,或者是什么,但是当我通过 vb.net 打开工作簿时,它打开时没有可见的菜单或功能区,您甚至无法访问它们。它看起来像是 Excel 的某种安全版本。如何让它正常打开?
这是我的完整块:(相关导入是 Microsoft.Office.Interop.Excel
Private Sub ButtonExcel_Click(sender As Object, e As EventArgs) Handles ButtonExcel.Click
Cursor = Cursors.WaitCursor
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
Dim oExcel As Excel.Application = Nothing
Dim oBook As Excel.Workbook = Nothing
Dim oRosterSheet As Excel.Worksheet = Nothing
oExcel = New Excel.Application
oExcel.Visible = True
oBook = oExcel.Workbooks.Open("C:\****.xlsb", [ReadOnly]:=True)
oRosterSheet = oBook.Worksheets(1)
oRosterSheet.Cells.Clear()
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In RosterTable.Columns
colIndex += 1
oRosterSheet.Cells(1, colIndex) = dc.ColumnName
Next dc
For Each dr In RosterTable.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In RosterTable.Columns
colIndex = colIndex + 1
oRosterSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next dc
Next dr
Dim fileName As String = "C:\****.xlsb"
oRosterSheet.Columns.AutoFit()
oBook.SaveAs(Filename:=fileName)
oExcel.UserControl = True
ReleaseObject(oRosterSheet)
ReleaseObject(oBook)
ReleaseObject(oExcel)
GC.Collect()
Cursor = Cursors.Default
End Sub
想通了。原来它是在 "Full Screen" 模式下打开 Excel,但不知何故没有最大化。可能与我正在远程访问的计算机上编码有关。无论哪种方式,下面的行修复了它:
oExcel.DisplayFullScreen = False