如何使用getActiveObject("Excel.Application") [英] How to use getActiveObject("Excel.Application")

查看:491
本文介绍了如何使用getActiveObject("Excel.Application")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用C#完成应用程序.

I need to finish an application in C#.

现在,我想获得一个控制Excel文件获取数据的函数.

Now I want to get a function that control a Excel file to get data.

我使用了getActiveObject("Excel.Application"),但这什么也没返回.我不能在VS2008中使用Excel.Application,而是使用Microsoft.Office.Interop.Excel.Application.

I used getActiveObject("Excel.Application"), but this returns nothing. I can't use Excel.Application in VS2008, and Microsoft.Office.Interop.Excel.Application is used instead.

那么还有另一种方法可以做到这一点吗?

So is there another way to do this?

Microsoft.Office.Interop.Excel.Application e = (Microsoft.Office.Interop.Excel.Application)Marshal.GetActiveObject("Excel.Application");

推荐答案

经过大约半天的闲逛,我终于弄清楚了如何进行这项工作,以便可以使用excel的开放副本.我的用户一直抱怨打开过多的Excel实例.

After about a half a day of playing around I finally figured out how to make this work so you can latch onto an open copy of excel. My users have been complaining mightily about having too many instances of Excel open.

以下是我为使其正常工作所做的摘要:

Here is a snippet of what I did to get it working:

_Application excelApp;

try
{
  excelApp = (_Application)Marshal.GetActiveObject("Excel.Application");
}
catch(Exception)
{
  // this is important. If Excel is not running, GetActiveObject will throw
  // an exception
  excelApp = null;
}

if( excelApp == null )
{
  excelApp = new ApplicationClass();
}

我已经追了好一阵子了,终于有一段时间想着要解决这个问题.

I have been chasing this for a while and finally had some time to hunker down and figure it out.

这篇关于如何使用getActiveObject("Excel.Application")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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