检查已安装的 Excel 版本并启动它

发布于 2024-12-05 20:29:13 字数 1355 浏览 0 评论 0原文

我正在尝试在我的应用程序中启动具有特定参数(即附加 xla 和 xll)的 Microsoft Excel。

目前它工作正常,因为我的所有用户都只安装了 Office11 (=2003)。

我的公司打算切换到 Windows 7 和 Windows 7 Office 2010 和我逻辑上无法启动任何 excel,因为 .exe 不位于 C:\Program Files:\Microsoft Office\Office11\EXCEL.EXE

我在注册表中运行了快速检查看到我绝对可以检查当前安装的版本。还有很多文章解释如何获取当前安装的 Office 版本。

但是,我想知道是否可以找到直接提供 .exe 路径以启动 Excel 的任何内容(例如好的注册表项)。

使用我当前的计算机(Win XP x86,Office11),我可以在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\11.0\Excel\InstallRoot 中找到它

使用这个密钥,基本上,我可以找到一种解决方法来获取实际路径。问题:安装了 Office 2010 (= Office 14) 的 Windows 7 注册表中没有此类密钥。

你们知道有什么方法可以从 C# 启动当前安装的 Excel 吗?

仅供参考,这是当前的代码部分,从 x64 / x86 计算机启动 Office11:

    private void LaunchExcel(string arguments)
    {
        if (!Is64BitsOS())
        {
            Process process = new Process();
            process.StartInfo.FileName = "excel";
            process.StartInfo.Arguments = arguments;
            process.Start();
        }
        else
        {
            Process process = new Process();
            process.StartInfo.FileName = "c:/Program Files (x86)/Microsoft Office/Office11/excel.exe";
            process.StartInfo.UseShellExecute = false;

            process.StartInfo.Arguments = arguments;
            process.Start();
        }
    }

有什么想法可以使此代码更通用吗?

I am trying in my application to launch Microsoft Excel with specific arguments (ie. additional xla & xll).

For now it works fine because all of my users only have Office11 (=2003) installed.

My company is going to switch to Windows 7 & Office 2010 and I logically can't launch any excel since the .exe is not located in C:\Program Files:\Microsoft Office\Office11\EXCEL.EXE

I ran a quick check in the registry to see that I can definitely check what version is currently installed. There are also plenty of articles explaining how to get the currently installed Office version.

However, I'd like to know if it is possible to find anything (such as a good registry key) directly giving me the .exe path so as to launch Excel.

Using my current machine (Win XP x86, Office11), I can find it in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\11.0\Excel\InstallRoot

Using this key I can, basically, find a workaround to get the actual path. Problem: there is no such key in Windows 7's registry with Office 2010 (= Office 14) installed.

Do you guys know any way to launch the currently installed excel from C#?

FYI, here is the current code section, launching Office11 from a x64 / x86 machine:

    private void LaunchExcel(string arguments)
    {
        if (!Is64BitsOS())
        {
            Process process = new Process();
            process.StartInfo.FileName = "excel";
            process.StartInfo.Arguments = arguments;
            process.Start();
        }
        else
        {
            Process process = new Process();
            process.StartInfo.FileName = "c:/Program Files (x86)/Microsoft Office/Office11/excel.exe";
            process.StartInfo.UseShellExecute = false;

            process.StartInfo.Arguments = arguments;
            process.Start();
        }
    }

Any ideas to make this code more generic?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘味果▽酱 2024-12-12 20:29:13

如果启动 Excel 来打开 Excel 文件,则可以使用 Excel 文件作为 FileName 启动一个 Process,并让 Windows shell 完成所有工作来查找关联的应用程序。显然,您需要一个异常处理程序。

这将使您独立于 Office 和 Windows 版本以及注册表项。

否则,您可以采取不同的方法并找到关联的应用程序,例如 此处

这些建议的要点是:目前,一旦安装新的 Office 版本或使用不同的 Windows 版本,您就必须更改代码,但有一种方法可以避免这些依赖关系。

If you start Excel to open an Excel file, you can start a Process with the Excel file as FileName and let the Windows shell do all the work to find the associated application. You'd need an exception handler, obviously.

This would make you independent of Office and Windows versions and registry keys.

Otherwise you could take a different approach and find the associated application, like here.

The point of these suggestions is: presently, you have to change your code as soon as a new Office version is installed or a different Windows version is used, while there is a way to avoid these dependencies.

放手` 2024-12-12 20:29:13

在 64 位版本的 Windows(XPx64、Vistax64、Win7x64)上运行的 32 位版本的 Excel 2010 将具有以下密钥。
我想这就是你正在寻找的钥匙
HKLM\SOFTWARE\Wow6432Node\Microsoft\Office\14.0\Excel\InstallRoot

在 64 位版本的 Windows(XPx64、Vistax64、Win7x64)上运行的 64 位版本的 Excel 2010 将具有以下密钥

<代码>HKLM\SOFTWARE\Microsoft\Office\14.0\Excel\InstallRoot

复制自 此处

The 32 bit version of Excel 2010 running on a 64 bit version of Windows (XPx64,Vistax64,Win7x64) will have the following key.
I think this is the key you are looking for
HKLM\SOFTWARE\Wow6432Node\Microsoft\Office\14.0\Excel\InstallRoot

The 64 bit version of Excel 2010 running on a 64 bit version of Windows (XPx64,Vistax64,Win7x64) will have the following key

HKLM\SOFTWARE\Microsoft\Office\14.0\Excel\InstallRoot

copied from here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文