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

时间:2015-09-04 13:32:20

标签: c# .net import-from-excel

我使用的是Vs2013。我创建了应用程序,其中我使用Excel文件作为输入并从文件中获取联系人。 一切都在我的电脑里工作。我有Vs2013。 Windows 8.1,Ms office 2007& 2013年
当我在任何其他计算机上运行我的应用程序时,它会抛出

  

无法加载文件或程序集'office,Version = 15.0.0.0,Culture = neutral,PublicKeyToken = 71e9bc111e9429c'或其中一个依赖项。系统找不到指定的文件

根据我的应用程序要求,我需要使用Office 2007到2013的Excel文件。

我已经提到了几个StackOverflow链接,但我没有得到结果。我被卡住了。请建议我如何解决这个问题。

5 个答案:

答案 0 :(得分:8)

您的其他计算机需要安装相应版本的Office。 15.0.0.0应对应于Office 2013 - 需要在目标计算机上安装(其他版本的Office可能无法运行)。这几乎可以肯定意味着您正在使用MSOffice互操作库,只有在安装了办公室且版本相同的情况下才能使用它。

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

答案 1 :(得分:5)

我通过更改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

答案 2 :(得分:1)

我创建了一个批处理文件来解决此问题。见下面的脚本:

    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\16.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=16
    if exist c:\windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=15
    if exist c:\windows\assembly\GAC_MSIL\office\14.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=14

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

答案 3 :(得分:1)

即使我有 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\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll 和 C:\Windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\OFFICE.DLL

答案 4 :(得分:0)

我遇到了同样的问题,您应该包含以下包 (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();
                }