My Winforms program always creates an instance of Excel (even without using Excel in the program)

john noble 181 Reputation points
2023-04-27T15:45:38.5833333+00:00

I have a winforms program. In certain parts of the program there is the ability to export data to Excel. To do this I have added a reference to Microsoft.Office.Interop.Excel. This seems to create an instance of excel that runs in the background (even before I use any of the Excel related programs within the app).

I have noticed that on PCs where MS Office is not installed, having this background instance of Excel running really slows down the performance of the program. It can take up to a minute just to load all the buttons, labels etc on a form. (It takes less than a second normally). As soon as I End the Excel Process in the Task Manager, the program loads up instantly.

Is there anyway I can have a reference to the Microsoft.Office.Interop.Excel without it starting an actual Instance of Excel in the background ?

Regards,

John

Developer technologies Windows Forms
Microsoft 365 and Office Excel For business Windows
Developer technologies C#
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-04-27T17:23:14.59+00:00

    don't call:

        var excelApp = new Excel.Application();
    

    until you need it.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. john noble 181 Reputation points
    2023-04-28T12:16:37.5+00:00

    Hi folks,

    I think I have tracked down the source of the problem....

    try
    {
         Type officeType = Type.GetTypeFromProgID("Excel.Application");
         dynamic xlApp = Activator.CreateInstance(officeType);
         xlApp.Visible = false;
         pathToExcel = xlApp.Path + @"\Excel.exe";
         xlApp.Quit();
         xlApp.Dispose();
    }
    

    In my code, I try and get the path to the Excel exe (as some PCs may have had 32 / 64 bit versions). Once I had the path, I could then just call Start.Process(pathtoExcel). It would seem even calling xlApp.Quit() and xlApp.Dispose() does not kill the process.

    But now, I have reconfigured my code so that I dont need to know the path anymore.

    Thanks again,

    John

    0 comments No comments

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.