Embedded Excel in WebBrowser (c#) - avoid separate

2019-08-18 02:36发布

I am using the WebBrowser control (i.e. from System.Window.Controls) to host Excel in my C# WPF application.

Annoyingly, it will open in a new window unless I manually change the BrowserFlags key from value 0x00000008 to 0x80000A00 for a number of HKLM keys (below is a .reg files I've created to do this quickly). With this registry change the Excel application will open inside the c# application WebBrowser control as expected.

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.OpenDocumentSpreadsheet.12]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Sheet.12]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Sheet.8]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.SheetBinaryMacroEnabled.12]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.SheetMacroEnabled.12]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Template.8]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.TemplateMacroEnabled]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Workspace]
"BrowserFlags"=dword:80000A00

Note that these registry changes are undocumented in the latest Excel (there used to be an MSDN registry article to do this for earlier Excel, but there is no official word on doing this for newer Excel versions but it works).

Now annoyingly, every now and again, Excel updates will revert the registry to their default values. Therefore a well functioning application needs to be able to set the registry value appropriately upon each launch (and put the value back to the previous setting upon closing if polite).

However, this means changing HKLM which requires UAC prompting, or giving the application admin privileges (both of which are undesirable).

Does anyone know a better way of achieving this? The route I would guess is looking at the AxIWebBrowser2 property of the WebBrowser and doing a lower level configuration of how Excel is embedded, but this goes beyond my knowledge. Any ideas? Note I don't want to go down the ActiveX commercial controls approach since the WebBrowser seems very stable and is Microsoft's recommended way of embedding Excel (and hence should be more future proof).

0条回答
登录 后发表回答