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

问题描述 投票:0回答:1

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

在未知模块中发生了'System.IO.FileNotFoundException类型的未处理的异常。无法加载文件或程序集“办公室,版本= 15.0.0.0,区域性=中性,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);
            }
        }
    }
}

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

c# excel excel-interop
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.