简体   繁体   中英

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

Attempting to load multiple .xlsx files. After updating Office versions to 2013 to make sure the .dll was compatible, I'm still getting this error.

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.

When attempting to execute the following code.

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);
            }
        }
    }
}

The plan ultimately is to create pivot tables in a new file based off the data, but I can't even execute opening and closing the files. The files DO exist at the actual file paths. All help is greatly appreciated!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM