无法打开另一个excel文件(当一个excel被.net打开) [英] Unable to open another excel file (when one excel is opened by .net)

查看:149
本文介绍了无法打开另一个excel文件(当一个excel被.net打开)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个.net应用程序,它将在登录时打开一个excel文件,并使用它来打印报告。注销用户时将关闭它。我为excel文件设置为false,以便用户不了解后台进程。

I have designed a .net application which will open an excel file at the time of login and use it to print a report. It will be closed while logging out the user. I set visible to false for excel file, so that user doesn't know about the background process.

但是如果有人在此期间打开任何其他excel文件,我的报告excel文件变得可见,并且Excel对象被折叠。我必须去任务经理并杀死所有打开的excel实例来解决这个问题。

But if anybody opens any other excel file during this time, my report excel file becomes visible and the excel object is collapsed. I have to go to task manager and kill the all open excel instances to fix this.

代码

 Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
        Try
            Dim dt As New DataTable()
            Dim Adapter As New SqlDataAdapter()
            ConnectMe()
            Dim SQLCmd As New SqlCommand("uspLogin", Con)
            SQLCmd.CommandType = CommandType.StoredProcedure
            SQLCmd.Parameters.AddWithValue("@pLoginName", UsernameTextBox.Text.Trim())
            SQLCmd.Parameters.AddWithValue("@pPassword", PasswordTextBox.Text.Trim())
            Adapter.SelectCommand = SQLCmd
            Adapter.Fill(dt)
            SQLCmd.Dispose()
            If dt.Rows.Count > 0 Then
                Me.Cursor = Cursors.WaitCursor
                Loading.Show()
                OpenAllTempaltes()            
                Me.Hide()                
                Con.Close()
                Me.Cursor = Cursors.Arrow
            Else
                MsgBox("Your Credential is Wrong !!!", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Login")
                UsernameTextBox.Text = ""
                PasswordTextBox.Text = ""              
                UsernameTextBox.Focus()
            End If
        Catch ex As Exception         
            Application.Exit()
        End Try
    End Sub

 Public Sub OpenAllTempaltes()
        Try                                   
            xlWorkBook = xlApp.Workbooks.Open(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Templates", "Excel_Templates_GST.xlsm"), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, True)                
        Catch ex As Exception
            Throw
        End Try
    End Sub

    Public Sub CloseAllTempaltes()
        Try
            CleanUp(xlApp, xlWorkBook, xlWorkSheet)
        Catch ex As Exception
            ExceptionLog("PrintPage", "CloseAllTempaltes", ex.ToString(), DateTime.Now.ToString("dd-MMM-yyyy"))
        Finally
            GC.Collect()
        End Try
    End Sub

请帮助我了解如何防止这种情况。

Please help me out on how to prevent this.

推荐答案

使用 IgnoreRemoteRequests Excel应用程序对象的属性:

Use the IgnoreRemoteRequests property of the Excel application object:

xlApp.IgnoreRemoteRequests = True

这相当于在

Fi上检查Excel UI选项le |选项|高级|一般|忽略使用动态数据交换(DDE)的其他应用程序。

(请参阅此相关答案上的SuperUser 。)

This is the equivalent of checking the Excel UI option at
File | Options | Advanced | General | Ignore other applications that use Dynamic Data Exchange (DDE).
(See this related answer on SuperUser.)

我无法通过.NET应用程序方便地重现您的场景,但通过晚期将Word.Application对象从Word VBA绑定,并运行一些测试,它的工作原理意。我创建了一个隐藏的Excel应用程序,并且通过在文件资源管理器中双击可以在打开文件之前和之后执行操作。

I couldn't conveniently reproduce your scenario with a .NET application, but ran some tests by late binding an Excel.Application object from Word VBA and it worked as intended. I created a hidden Excel application, and was able to perform actions on it before and after opening files by double-clicking in File Explorer.

在我的测试中,下次平常打开Excel时,设置尚未切换,但您可能希望捕获其值并在退出应用程序对象之前还原它,以防这种行为不是通用的。

In my tests, the setting was not still toggled on the next time I opened Excel normally, but you might want to capture its value and restore it before quitting your application object, in case that behavior isn't universal.

编辑:自从至少Excel 2003以来,这种行为一直存在,我使用Excel 2016(32位)进行了验证。

This behavior has been around since at least Excel 2003, and I verified using Excel 2016 (32-bit).

在Excel 2013或更高版本中,Excel切换到单个文档界面:每个工作簿都在自己的窗口中打开。

In Excel 2013 or later, Excel switched to a single document interface: each workbook opens in its own window.

至少在2016年,Visual Basic编辑器仍然是一个多文档界面,您可以通过查看Project Explorer来轻松查看应用程序会话中打开的文件VBE中的窗格。

At least through 2016, the Visual Basic Editor has remained a multiple document interface, and you can easily see which files are open in an application session by looking at the Project Explorer pane in the VBE.

这篇关于无法打开另一个excel文件(当一个excel被.net打开)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆