ASP.NET,无法获取已经打开的Excel实例 [英] ASP.NET, Unable to get the Already opened Excel Instance
本文介绍了ASP.NET,无法获取已经打开的Excel实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
功能 IsExcelRunning()作为 Boolean
Dim xlApp As Excel.Application
开 错误 恢复 下一步
xlApp = GetObject(, Excel.Application)
IsExcelRunning =(Err.Number = 0 )
xlApp = 没什么
Err.Clear()
结束 功能
功能 OpenExcel()
如果 IsExcelRunning()然后
excelAp p = GetObject(, Excel.Application)
Else
excelApp = CreateObject( Excel.Application)
结束 如果
结束 函数
上面的代码总是调用CreateObject方法而不是在IIS 6(Windows Server 2008)中托管时的GetObject。但是,在调试模式下,一旦CreateObject方法调用它就调用GetObject。
问题是,每次我们调用OpenExcel函数时,在服务器中打开一个单独的Excel实例。请指导如何避免它。
解决方案
此代码用c#编写。 excelWorkbook对象是当前打开的工作簿的实例。
Microsoft.Office.Interop.Excel.Workbook excelWorkbook =(Microsoft.Office .Interop.Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
Function IsExcelRunning() As Boolean
Dim xlApp As Excel.Application
On Error Resume Next
xlApp = GetObject(, "Excel.Application")
IsExcelRunning = (Err.Number = 0)
xlApp = Nothing
Err.Clear()
End Function
Function OpenExcel ()
If IsExcelRunning() Then
excelApp = GetObject(, "Excel.Application")
Else
excelApp = CreateObject("Excel.Application")
End If
End Function
The Above code always calling CreateObject method instead of GetObject when hosted in IIS 6 (Windows Server 2008). However, in debug mode it calls the GetObject once the CreateObject method calls.
The issue is, each time we call the OpenExcel function one separate Excel instance opening in server. Please guide how to avoid it.
解决方案
This code is written in c#. excelWorkbook object is the instance of the currently opened workbook.
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = (Microsoft.Office.Interop.Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
这篇关于ASP.NET,无法获取已经打开的Excel实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文