尝试加载多个 .xlsx 文件。将 Office 版本更新到 2013 以确保 .dll 兼容后,我仍然收到此错误。
未知模块中发生了“System.IO.FileNotFoundException”类型的未处理异常。无法加载文件或程序集“office,版本=15.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c”。该系统找不到指定的文件。
尝试执行以下代码时。
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);
}
}
}
}
最终计划是根据数据在新文件中创建数据透视表,但我什至无法执行打开和关闭文件。这些文件确实存在于实际文件路径中。非常感谢所有帮助!