Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何确保使用VSTO 4打开哪个版本的Excel?_.net_Excel_Vsto - Fatal编程技术网

.net 如何确保使用VSTO 4打开哪个版本的Excel?

.net 如何确保使用VSTO 4打开哪个版本的Excel?,.net,excel,vsto,.net,Excel,Vsto,我使用VSTO填充Excel工作表,如下所示: Application app = new Application(); var wBook = app.Workbooks.Add(); var wSheet = (wBook.Worksheets[1] as Worksheet); /* Population algorithm */ app.Visible=true; 工作表已经创建,一切都很好,只是在我工作的环境中安装了两个版本的Excel(Excel 2003和Excel 2010)

我使用VSTO填充Excel工作表,如下所示:

Application app = new Application();
var wBook = app.Workbooks.Add();
var wSheet = (wBook.Worksheets[1] as Worksheet);
/* Population algorithm */
app.Visible=true;
工作表已经创建,一切都很好,只是在我工作的环境中安装了两个版本的Excel(Excel 2003和Excel 2010)

上周,当我第一次创建代码时,Excel 2010出现了。然而,本周,是Excel 2003打开了

我的项目引用了Microsoft.Office.Interop.Excel的最新版本(
C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll
,版本14)


这是Windows中的首选项,还是我在创建应用程序实例时必须指定的内容?

也许可以启动excel 2010并处理它,下面的代码可能会有所帮助

 public static void GetReferences(ref Microsoft.Office.Interop.Excel.Application _Application, ref Microsoft.Office.Interop.Excel.Workbook _Workbook) 
{ 
    EnumChildCallback cb; 
    // start exe
    int hwnd = Process.Start("Excel 2010 path").MainWindowHandle; 


    if (hwnd != 0) 
    { 
        int hwndChild = 0; 
        cb = new EnumChildCallback(EnumChildProc); 
        EnumChildWindows(hwnd, cb, ref hwndChild); 


        if (hwndChild != 0) 
        { 
            const uint OBJID_NATIVEOM = 0xFFFFFFF0; 
            Guid IID_IDispatch = new Guid( 
                 "{00020400-0000-0000-C000-000000000046}"); 
            Microsoft.Office.Interop.Excel.Window ptr = null; 

            int hr = AccessibleObjectFromWindow( 
                  hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref ptr); 
            if (hr >= 0) 
            {                
                _Application = ptr.Application; 
                _Workbook = _Application.ActiveWorkbook; 
            } 
        } 
    } 
} 

[DllImport("Oleacc.dll")] 
public static extern int AccessibleObjectFromWindow( 
      int hwnd, uint dwObjectID, byte[] riid, 
      ref Microsoft.Office.Interop.Excel.Window ptr); 

public delegate bool EnumChildCallback(int hwnd, ref int lParam); 

[DllImport("User32.dll")] 
public static extern bool EnumChildWindows( 
      int hWndParent, EnumChildCallback lpEnumFunc, 
      ref int lParam); 


[DllImport("User32.dll")] 
public static extern int GetClassName( 
      int hWnd, StringBuilder lpClassName, int nMaxCount); 

public static bool EnumChildProc(int hwndChild, ref int lParam) 
{ 
    StringBuilder buf = new StringBuilder(128); 
    GetClassName(hwndChild, buf, 128); 
    if (buf.ToString() == "EXCEL7") 
    { 
        lParam = hwndChild; 
        return false; 
    } 
    return true; 

}

或者,您可以将目标锁定在2003年的PIO上,然后打开哪个Excel就无关紧要了。@Jesse我不明白,什么是PIO?另外,我想让2010年开幕。对不起,皮亚。您引用的dll。它们是向前兼容的,即2003年的PIA将自动执行2010年的操作,但您将无法参考新功能。您当前的office版本是2003年。修复office 2010将解决您的问题。。。。