C# 在未知模块中使用未处理的互操作 'System.IO.FileNotFoundException' 加载 Excel 文件

C# Loading Excel File Using Interop Unhandled 'System.IO.FileNotFoundException' in Unknown Module

正在尝试加载多个 .xlsx 文件。将 Office 版本更新到 2013 以确保 .dll 兼容后,我仍然收到此错误。

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module. Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.

尝试执行以下代码时。

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

namespace SolnName
{
    class Program
    {
        static void Main(string[] args)
        {
            //File paths
            string path1 = @"path\file1.xlsx";
            string path2 = @"path\file2.xlsx";

            try
            {
                Excel.Application xlApp = new Excel.Application();

                Excel.Workbook book1 = xlApp.Workbooks.Open(path1);
                Excel.Worksheet sheet1 = (Excel.Worksheet)book1.Worksheets.get_Item(1);

                Excel.Workbook book2 = xlApp.Workbooks.Open(path2);
                Excel.Worksheet sheet2 = (Excel.Worksheet)book2.Worksheets.get_Item(1);

                book1.Close(true, null, null);
                book2.Close(true, null, null);

                xlApp.Quit();

                Marshal.ReleaseComObject(sheet1);
                Marshal.ReleaseComObject(sheet2);
                Marshal.ReleaseComObject(book1);
                Marshal.ReleaseComObject(book2);
                Marshal.ReleaseComObject(xlApp);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.Data);
                Console.WriteLine(e.Message);
            }
        }
    }
}

最终计划是根据数据在新文件中创建数据透视表,但我什至无法执行打开和关闭文件。这些文件确实存在于实际文件路径中。非常感谢所有帮助!

VS 项目的目标框架已关闭。