Share via

Programmatically open Excel file in .NET 6

Urs Wagner 26 Reputation points
Sep 29, 2022, 1:50 PM

Hello

I am using .NET 6 and I want to open the Excel file with Microsoft.Office.Interop.Excel.

Calling the constructor of my C# class I get this error:

System.IO.FileNotFoundException: 'Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.'

I have installed Microsoft.Interop.Excel Nuget package.

I have installed in Visual Studio the office tools.

I am using the same code with .NET Framework 4.8. It is working fine.

Must a use another library?

Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,852 questions
{count} votes

Accepted answer
  1. Sina Salam 18,881 Reputation points
    Oct 4, 2022, 11:45 AM

    Hi @Urs Wagner

    Here is the repo link:

    https://github.com/sasinaola/WpfAppExcel.git

    Success.
    Sina


5 additional answers

Sort by: Most helpful
  1. Sina Salam 18,881 Reputation points
    Sep 29, 2022, 2:09 PM

    @Urs Wagner

    In summary, you will need to install the version of Microsoft Office Excel on that system, that has not been expired.

    Your machine needs to have the corresponding version of Office installed. 15.0.0.0 should correspond to Office 2013 - that needs to be installed on your target machine (other versions of Office may not work). This almost certainly means you're using MSOffice interop libraries, which only work if office is installed and against the same version.

    Alternatively, you could refactor your code to just directly read the Excel XML.

    Let me know if the above is helpful. Then, we can move forward. I have a typical application that can call any Microsoft Office irrespective of their version.

    0 comments No comments

  2. Urs Wagner 26 Reputation points
    Sep 29, 2022, 2:13 PM

    Thank You for Your answer.
    Does this version 15.0.0.0 not work for Office 2016?
    We have Office 2016 installed. I can't find a newer Microsoft.Office.Interop.Excel library,$

    0 comments No comments

  3. Sina Salam 18,881 Reputation points
    Sep 30, 2022, 11:07 AM

    Hi @Urs Wagner

    Try to do this I strongly it will work, because it worked for me to invoke any version of Office. (Now for Excel in this context).

    1. Ensure the office is a licensed copy,
    2. Put this at to of your code: using Excel = Microsoft.Office.Interop.Excel;
    3. Configure your InteropService and put using System.Runtime.InteropServices;
    4. Check this snippet below:
      private void btnExcel_Click(object sender, EventArgs e)
      {
      Excel.Application objExcel = new Excel.Application();
      if ------- // you can put the time to activate the button when the Apps start. (it's optional)
      {
      objExcel.Visible = true;
      }
      else
      {
      //something to do or alert;
      }
          // More Code   
      }  
      

    Let me know how it goes.

    Regards,
    Sina


  4. Sina Salam 18,881 Reputation points
    Oct 4, 2022, 4:42 AM

    Hi @Urs Wagner

    I have have been doing this more than decades and it work fine.

    I created new app using .NET 7 preview VS 2022, while I also created WPF App with .NET 6 stable. Just because of you to affirm.

    Actually, my old applications works fine and the new one work fine too. However, see the below codes and steps:

    using Microsoft.Office.Interop;
    using ExcelAdaptorLib = Microsoft.Office.Interop.Excel;
    using Excel = Microsoft.Office.Interop.Excel;
    using ExcelAdaptorLib;

    namespace WpfAppExcel
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    }

        private void Button_Click(object sender, RoutedEventArgs e)  
        {  
            Excel.Application objExcel = new Excel.Application();  
            objExcel.Visible = true;  
        }  
    }  
    

    }

    Now, before the above code add your reference in COM

    Notice new version 16.0, also, check your package for alert, if there is any on security warning perhaps you add testing for excelTools, remove it for now.

    Wishing you good success, I can create a video of it on weekend.

    cheers and my regards,
    Sina


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.