Could not load file or assembly ‘office, Version=15.0.0.0 using Microsoft.Office.Interop.Excel

我是研究僧i 提交于 2020-12-13 17:54:52

问题


I'm using Microsoft.Office.Interop.Excel to convert excel to pdf. But when I start Excel application, this error happened. I already installed Excel 2013 on my computer. (I'm using VS2019, Window 10).

My Excel's location is at C\Program Files (x86)\Microsoft Office\Office 15\Excel.

Could not load file or assembly ‘office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxx’. The system cannot find the file specified

Any suggestions are welcomed!

This is my code:

using Microsoft.Office.Interop.Excel;
using System;
using System.Runtime.InteropServices;

namespace ExcelToPdf
{
    public class ExcelApplicationWrapper : IDisposable
    {
        public Application ExcelApplication { get; }

        public ExcelApplicationWrapper()
        {
            ExcelApplication = new Application(); // start excel application
        }

        public void Dispose()
        {
            // Each file I open is locked by the background EXCEL.exe until it is quitted
            ExcelApplication.Quit();
            Marshal.ReleaseComObject(ExcelApplication);
        }
    }
}
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

namespace ExcelToPdf
{
    public class ExcelInteropExcelToPdfConverter
    {
        public void ConvertToPdf(IEnumerable<string> excelFilesPathToConvert)
        {
            using (var excelApplication = new ExcelApplicationWrapper()) // got error here
            {

            }
        }
    }
}

回答1:


here is the answer https://github.com/dotnet/project-system/issues/5735

in order to fix the situation, the project system needs to add True.

Old:

<ItemGroup>
    <COMReference Include="Microsoft.Office.Excel.dll">
        <Guid>00020813-0000-0000-c000-000000000046</Guid>
        <VersionMajor>1</VersionMajor>
        <VersionMinor>7</VersionMinor>
        <WrapperTool>tlbimp</WrapperTool>
        <Lcid>0</Lcid>
        <Isolated>false</Isolated>
    </COMReference>
</ItemGroup>

New:

<ItemGroup>
    <COMReference Include="Microsoft.Office.Interop.Excel">
        <Guid>{00020813-0000-0000-C000-000000000046}</Guid>
        <VersionMajor>1</VersionMajor>
        <VersionMinor>7</VersionMinor>
        <Lcid>0</Lcid>
        <WrapperTool>primary</WrapperTool>
        <Isolated>False</Isolated>
        <EmbedInteropTypes>True</EmbedInteropTypes>
    </COMReference>
</ItemGroup>


来源:https://stackoverflow.com/questions/59067072/could-not-load-file-or-assembly-office-version-15-0-0-0-using-microsoft-office

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!