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

本文关键字:Version Office 程序集 文件 加载 | 更新日期: 2023-09-27 18:11:08

我使用Vs2013。我已经创建了应用程序,其中我使用Excel文件作为输入并从文件中获得联系。我的电脑一切正常。我有Vs2013。Windows 8.1, Ms office 2007 & &;2013年。
当我在其他计算机上运行我的应用程序时,它会抛出

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

文件

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

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

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

我也得到了这个错误信息,即使我有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

您的另一台机器需要安装相应版本的Office。15.0.0.0应该对应于Office 2013——它需要安装在你的目标机器上(其他版本的Office可能不工作)。这几乎肯定意味着你正在使用moffice互操作库,这些库只有在安装了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

我的问题是我试图使用nuget包:

<PackageReference Include="Microsoft.Office.Interop.Excel" Version="15.0.4795.1001" />

不仅是不支持的,它只是无法加载FileNotFoundException

使用comreference属性。例如,下面是Excel的COMReference:

<ItemGroup>
    <COMReference Include="Microsoft.Office.Interop.Excel">
        <EmbedInteropTypes>true</EmbedInteropTypes>
        <Guid>00020813-0000-0000-c000-000000000046</Guid>
        <Isolated>false</Isolated>
        <Lcid>0</Lcid>
        <WrapperTool>primary</WrapperTool>
        <VersionMajor>1</VersionMajor>
        <VersionMinor>9</VersionMinor>
    </COMReference>
</ItemGroup>

关于子属性的一点帮助:当使用Office互操作#5735

时,不正确的COMReference条目

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

    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

我有同样的问题,你应该包括以下包(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();
                }