我有一个 VB.NET 应用程序,它使用 Microsoft.Office.Interop.Excel 简单地打开电子表格,将一些数据输入其中,刷新数据透视表中的数据,然后向用户展示电子表格。
代码非常简单(未显示错误处理):
' Start Excel Application
xlApp = New Excel.ApplicationClass
' Get Excel Workbooks
xlWorkBooks = xlApp.Workbooks
' Open workbook
xlWorkBook = xlWorkBooks.Open(Filename:=sExcelDocFullPath, IgnoreReadOnlyRecommended:=blnIgnoreReadOnly)
If Not xlWorkBook Is Nothing Then
' Pump some data over (code not shown)
' Make the first worksheet in the document active
CType(xlWorkBook.Worksheets(1), Excel.Worksheet).Activate()
'Return control of Excel to the user
xlApp.Visible = True
xlApp.UserControl = True
' Refresh workbooks (with alerts off, so as not to hassle user )
xlApp.DisplayAlerts = False
xlWorkBook.RefreshAll()
End If
运行此代码时会发生什么情况,Excel 在调用应用程序后面打开,因为我们正在刷新数据连接,所以在调用应用程序后面也显示了小的 Excel“SQL Server 登录”对话框,因此用户需要 ALT +TAB 到 Excel 或单击任务栏中的 Excel - 对用户来说都不是很好的体验。我们希望登录对话框出现在调用应用程序的前面。
谁能建议如何实现这一目标?我尝试将 Visible 和 UserControl 属性的设置移到 RefreshAll 之后,但这没有任何区别。
干杯,
克里斯。