[c#] How do I properly clean up Excel interop objects?

Here is a really easy way to do it:

[DllImport("User32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
...

int objExcelProcessId = 0;

Excel.Application objExcel = new Excel.Application();

GetWindowThreadProcessId(new IntPtr(objExcel.Hwnd), out objExcelProcessId);

Process.GetProcessById(objExcelProcessId).Kill();

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to excel

Python: Pandas pd.read_excel giving ImportError: Install xlrd >= 0.9.0 for Excel support Converting unix time into date-time via excel How to increment a letter N times per iteration and store in an array? 'Microsoft.ACE.OLEDB.16.0' provider is not registered on the local machine. (System.Data) How to import an Excel file into SQL Server? Copy filtered data to another sheet using VBA Better way to find last used row Could pandas use column as index? Check if a value is in an array or not with Excel VBA How to sort dates from Oldest to Newest in Excel?

Examples related to interop

Exception from HRESULT: 0x800A03EC Error How to use Microsoft.Office.Interop.Excel on a machine without installed MS Office? How do I import from Excel to a DataSet using Microsoft.Office.Interop.Excel? How can I make SQL case sensitive string comparison on MySQL? Better way to cast object to int Write Array to Excel Range How do I properly clean up Excel interop objects? How to call shell commands from Ruby

Examples related to com-interop

Retrieving the COM class factory for component with CLSID {XXXX} failed due to the following error: 80040154 How do I properly clean up Excel interop objects?