无法加载文件或程序集 'Office, Version=15.0.0.0'

Could not load file or assembly 'Office, Version=15.0.0.0'

我用的是Vs2013。我创建了一个应用程序,在该应用程序中我使用 Excel 文件作为输入并从文件中获取联系人。 一切都在我的电脑上工作。我有 Vs2013。 Windows8.1,办公室女士 2007 和 2013。
当我 运行 我的应用程序在任何其他计算机上时,它会抛出

Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bc111e9429c' or one of its dependencies. The system cannot find the file specified

根据我的申请要求,我需要使用从 Office 2007 到 2013 的 Excel 个文件。

我参考了几个 Whosebug 链接,但没有得到结果。我卡住了。请建议我如何解决这个问题。

您的另一台机器需要安装相应版本的Office。 15.0.0.0 应对应于 Office 2013 - 需要在目标计算机上安装(其他版本的 Office 可能无法运行)。这几乎可以肯定意味着您正在使用 MSOffice 互操作库,只有在安装了 office 并且针对相同版本时它才有效。

或者,您可以重构代码以直接读取 Excel XML.

我通过更改 Excel.dll 版本得到了解决方案。我使用的是 15.0.0.0,现在我将其更改为 12.0.0.0,并且工作正常。 我从 Add reference > Browse > C: > Windows > assembly > GAC > Microsoft.Office.Interop.Excel > 12.0.0.0_etc > Microsoft.Office.Interop.Excel.dll

得到了 dll

我创建了一个批处理文件来解决这个问题。请参阅下面的脚本:

    echo off
        cls
        color 1f
        echo Checking for Administrator elevation.
        openfiles>nul 2>&1

            if %errorlevel% EQU 0 goto isadmin

                COLOR 4f
            echo.    You are not running as Administrator.
            echo.    This tool cannot do it's job without elevation.
            echo.
            echo.    You need run this tool as Administrator.
            echo.

            echo.Press any key to continue . . .
            pause>nul
        exit
        :isadmin
        if exist c:\windows\assembly\GAC_MSIL\office.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=16
    if exist c:\windows\assembly\GAC_MSIL\office.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=15
    if exist c:\windows\assembly\GAC_MSIL\office.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=14

    md c:\windows\assembly\GAC_MSIL\office.0.0.0__71e9bce111e9429c
    xcopy c:\windows\assembly\GAC_MSIL\office\%officever%.0.0.0__71e9bce111e9429c c:\windows\assembly\GAC_MSIL\office.0.0.0__71e9bce111e9429c /s/y
pause

即使我有 Office 2010 并且 GAC 下没有 Microsoft.Office.Interop.Excel 文件夹,我也收到此错误消息。
我在这里找到了适合我的案例的解决方案: https://www.add-in-express.com/forum/read.php?FID=5&TID=15525
您必须从这些文件夹中引用两个 dll 文件:
C:\Windows\assembly\GAC_MSIL\Microsoft.Vbe.Interop.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll

C:\Windows\assembly\GAC_MSIL\office.0.0.0__71e9bce111e9429c\OFFICE.DLL

我有同样的问题你应该包括以下包 (Syncfusion.XlsIO) 而不是 (Microsoft.Office.interop.Excel) 因为它只支持 excel 2013,但第一个 'Syncfusion.XlsIO' 确实支持 Excel 2016;

=> using Syncfusion.XlsIO;

下面是代码:

  using (ExcelEngine excelEngine = new ExcelEngine())
                {
                        IApplication application = excelEngine.Excel;
                        application.DefaultVersion = ExcelVersion.Excel2016;
                        IWorkbook workbook = application.Workbooks.Create(1);
                        IWorksheet worksheet = workbook.Worksheets[0];
                        //Adding text to a cell
                        for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
                        {
                           worksheet.Range[1, i].Text = dataGridView1.Columns[i - 1].HeaderText;
                        }

                        for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
                        {
                            for (int j = 0; j < dataGridView1.Columns.Count; j++)
                            {
                                worksheet.Range[i + 2, j + 1].Text = dataGridView1.Rows[i].Cells[j].Value.ToString();
                            }
                        }
                        //Saving the workbook to disk in XLSX format
                        Stream excelstream = File.Create(Path.GetFullPath(@"MyExcelFile.xlsx"));
                        workbook.SaveAs(excelstream);
                        excelstream.Dispose();
                }