1

我有一个 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 之后,但这没有任何区别。

干杯,

克里斯。

4

1 回答 1

2

对不起C#,我不懂VB,所以我不能自己翻译,但主要思想是使用WinAPI:

[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(IntPtr hWnd);

您可以从 xlApp 获取 hwnd:

BringWindowToTop((INtPtr)XlApp.Hwnd);
于 2016-01-07T17:11:24.843 回答