37

I'm having an issue with Excel Interop.

The Excel.exe doesn't close even if when I realease instances.

Here is my code :

using xl = Microsoft.Office.Interop.Excel;


xl.Application excel = new xl.Application();
excel.Visible = true;
excel.ScreenUpdating = false;
if (wordFile.Contains(".csv") || wordFile.Contains(".xls"))
{
   //typeExcel become a string of the document name
   string typeExcel = wordFile.ToString();
   xl.Workbook workbook = excel.Workbooks.Open(typeExcel,
                                                oMissing,  oMissing,  oMissing,  oMissing,
                                                oMissing,  oMissing,  oMissing,  oMissing,
                                                oMissing,  oMissing,  oMissing,  oMissing,
                                                oMissing,  oMissing);
   object outputFileName = null;
   if (wordFile.Contains(".xls"))
   {
     outputFileName = wordFile.Replace(".xls", ".pdf");
   }
   else if (wordFile.Contains(".csv"))
   {
     outputFileName = wordFile.Replace(".csv", ".pdf");
   }

   workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, outputFileName, 
                                 XlFixedFormatQuality.xlQualityStandard, oMissing,
                                 oMissing, oMissing, oMissing, oMissing, oMissing);

   object saveChanges = xl.XlSaveAction.xlDoNotSaveChanges;
   ((xl._Workbook)workbook).Close(saveChanges, oMissing, oMissing);

   Marshal.ReleaseComObject(workbook);
   workbook = null;
}

I saw that, with the Marshal.RealeaseComObject it should be work, but nothing. How can I fix this?

Thank you.

Marcus Mangelsdorf
  • 2,852
  • 1
  • 30
  • 40
provençal le breton
  • 1,428
  • 4
  • 26
  • 43

16 Answers16

77

Simple rule: avoid using double-dot-calling expressions, such as this:

var workbook = excel.Workbooks.Open(/*params*/)

...because in this way you create RCW objects not only for workbook, but for Workbooks, and you should release it too (which is not possible if a reference to the object is not maintained).

So, the right way will be:

var workbooks = excel.Workbooks;
var workbook = workbooks.Open(/*params*/)

//business logic here

Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(excel);
CJBS
  • 15,147
  • 6
  • 86
  • 135
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
  • So, we(you) point the fact that, for a more efficiency in OfficeInterop, we have to do all the work, step by step and not "directly". That's good to know. That works this way. Thank you. – provençal le breton Jun 28 '13 at 14:57
  • Should I need to do the .close, and the .quit, when I do the release? Just to know if I can avoid it(self knowledge). – provençal le breton Jun 28 '13 at 14:58
  • 1
    @Jean: it is always recommended that you quit and close all the objects you have created.They might create the problem when you run the application next time. – Milind Anantwar Jun 28 '13 at 15:02
  • After couple of months I finally encounter with this answer, thank god. I think this situation should have been mentioned already in Microsoft Documentation for Marshal Class as an example. How could I know that and why did I have to search this at least 2 months. – Ardahan Kisbet Mar 25 '20 at 11:52
  • 4
    `Simple rule: avoid using double-dot-calling expressions, such as this` - this has been [shown to be false](https://stackoverflow.com/a/25135685/11683). – GSerg Apr 13 '20 at 06:56
  • I've done several tests and these are my conclusions: 1. The code above by itself is not enough, the process remains in memory. 2. You need to call `excelApp.Quit()` before the above. That does the trick. 3. Apparently it is *indeed* necessary to store a variable for the `workbooks`. I've first removed it and called `excelApp.Workbooks.Open(...` and `Marshal.ReleaseComObject(excelApp.Workbooks)`, but the process also remained in memory. It seems calling `.Workbooks` creates objects in memory which stay... 4. Finally, no need to do `ReleaseComObject(workbook)`, just `workbooks`. – Andrew Apr 24 '21 at 02:16
26

Here is a snippet of code I wrote, because I had the same problem as you. Basically, you need to close the workbook, quit the application, and then release ALL of your COM objects (not just the Excel Application object). Finally, call the garbage collector for good measure.

    /// <summary>
    /// Disposes the current <see cref="ExcelGraph" /> object and cleans up any resources.
    /// </summary>
    public void Dispose()
    {
        // Cleanup
        xWorkbook.Close(false);
        xApp.Quit();

        // Manual disposal because of COM
        while (Marshal.ReleaseComObject(xApp) != 0) { }
        while (Marshal.ReleaseComObject(xWorkbook) != 0) { }
        while (Marshal.ReleaseComObject(xWorksheets) != 0) { }
        while (Marshal.ReleaseComObject(xWorksheet) != 0) { }
        while (Marshal.ReleaseComObject(xCharts) != 0) { }
        while (Marshal.ReleaseComObject(xMyChart) != 0) { }
        while (Marshal.ReleaseComObject(xGraph) != 0) { }
        while (Marshal.ReleaseComObject(xSeriesColl) != 0) { }
        while (Marshal.ReleaseComObject(xSeries) != 0) { }
        xApp = null;
        xWorkbook = null;
        xWorksheets = null;
        xWorksheet = null;
        xCharts = null;
        xMyChart = null;
        xGraph = null;
        xSeriesColl = null;
        xSeries = null;

        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
qJake
  • 16,821
  • 17
  • 83
  • 135
  • 4
    Nice. It's also worth noting that I also had to call ReleaseComObject on Excel Range objects that I was using within a loop. – Gordon Glas Jun 12 '15 at 20:07
  • Your idea with `while` is great! – KUL Aug 10 '20 at 00:42
  • In my case, `ReleaseComObject` could return `-1` (and I got an infinite loop). I changed my loop to run while the return value was greater than 0, instead. – Tyler Montney Aug 26 '22 at 17:40
8

Rules - never use no more that one dot

-- one dot

var range = ((Range)xlWorksheet.Cells[rowIndex, setColumn]);
var hyperLinks = range.Hyperlinks;
hyperLinks.Add(range, data);

-- Two or more dots

 (Range)xlWorksheet.Cells[rowIndex, setColumn]).Hyperlinks.Add(range, data);

-- Example

 using Microsoft.Office.Interop.Excel;

 Application xls = null;
 Workbooks workBooks = null;
 Workbook workBook = null;
 Sheets sheets = null;
 Worksheet workSheet1 = null;
 Worksheet workSheet2 = null;

 workBooks = xls.Workbooks;
 workBook = workBooks.Open(workSpaceFile);
 sheets = workBook.Worksheets;
 workSheet1 = (Worksheet)sheets[1];


// removing from Memory
 if (xls != null)
 {    
   foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in sheets)
   {
      ReleaseObject(sheet);
   }

   ReleaseObject(sheets);
   workBook.Close();
   ReleaseObject(workBook);
   ReleaseObject(workBooks);

   xls.Application.Quit(); // THIS IS WHAT IS CAUSES EXCEL TO CLOSE
   xls.Quit();
   ReleaseObject(xls);

   sheets = null;
   workBook = null;
   workBooks = null;
   xls = null;

   GC.Collect();
   GC.WaitForPendingFinalizers();
   GC.Collect();
   GC.WaitForPendingFinalizers();
}
SztupY
  • 10,291
  • 8
  • 64
  • 87
Lawrence Thurman
  • 657
  • 8
  • 15
8

It is tricky to get rid of all references since you have to guess if calls like:

var workbook = excel.Workbooks.Open("")

Creates an instance of Workbooks that you do not hold a reference to.

Even references like:

targetRange.Columns.AutoFit()

Will create an instance of .Columns() without you knowing and not released properly.

I ended up writing a class holding a list of object references that could dispose all objects in reverse order.

The class has a list of objects and Add() functions for anything you reference as you use Excel interop that returns the object itself:

    public List<Object> _interopObjectList = new List<Object>();

    public Excel.Application add(Excel.Application obj)
    {
        _interopObjectList.Add(obj);
        return obj;
    }

    public Excel.Range add(Excel.Range obj)
    {
        _interopObjectList.Add(obj);
        return obj;
    }

    public Excel.Workbook add(Excel.Workbook obj)
    {
        _interopObjectList.Add(obj);
        return obj;
    }

    public Excel.Worksheet add(Excel.Worksheet obj)
    {
        _interopObjectList.Add(obj);
        return obj;
    }

    public Excel.Worksheets add(Excel.Worksheets obj)
    {
        _interopObjectList.Add(obj);
        return obj;
    }

    public Excel.Sheets add(Excel.Sheets obj)
    {
        _interopObjectList.Add(obj);
        return obj;
    }


    public Excel.Workbooks add(Excel.Workbooks obj)
    {
        _interopObjectList.Add(obj);
        return obj;
    }

Then to unregister objects I used the following code:

    //Release all registered interop objects in reverse order
    public void unregister()
    {
        //Loop object list in reverse order and release Office object
        for (int i=_interopObjectList.Count-1; i>=0 ; i -= 1)
        { ReleaseComObject(_interopObjectList[i]); }

        //Clear object list
        _interopObjectList.Clear();
    }


    /// <summary>
    /// Release a com interop object 
    /// </summary>
    /// <param name="obj"></param>
     public static void ReleaseComObject(object obj)
     {
         if (obj != null && InteropServices.Marshal.IsComObject(obj))
             try
             {
                 InteropServices.Marshal.FinalReleaseComObject(obj);
             }
             catch { }
             finally
             {
                 obj = null;
             }

         GC.Collect();
         GC.WaitForPendingFinalizers();
         GC.Collect();
         GC.WaitForPendingFinalizers();
     }

Then principle is to create the class and capture references like this:

//Create helper class
xlsHandler xlObj = new xlsHandler();

..

//Sample - Capture reference to excel application
Excel.Application _excelApp = xlObj.add(new Excel.Application());

..
//Sample - Call .Autofit() on a cell range and capture reference to .Columns() 
xlObj.add(_targetCell.Columns).AutoFit();

..

//Release all objects collected by helper class
xlObj.unregister();

Not perhaps code of great beauty but may inspire to something useful.

umlcat
  • 4,091
  • 3
  • 19
  • 29
flodis
  • 1,123
  • 13
  • 9
7

In your code you have:

excel.Workbooks.Open(...)

excel.Workbooks is creating a COM object. You are then calling the Open function from that COM object. You are not, however, releasing the COM object when you have finished.

This is a common issue when dealing with COM objects. Basically, you should never have more than one dot in your expression because you will need to clean up the COM objects when you've finished.

The topic is simply too big to explore completely in an answer, but I think you'll find Jake Ginnivan's article on the subject extremely helpful: VSTO and COM Interop

If you get tired of all those ReleaseComObject calls, you may find this question helpful:
How to properly clean up Excel interop object in C#, 2012 edition

Community
  • 1
  • 1
JDB
  • 25,172
  • 5
  • 72
  • 123
5

As stated in other answers, using two dots will create hidden references that cannot be closed by Marshal.FinalReleaseComObject. I just wanted to share my solution, which eliminates the need to remember Marshal.FinalReleaseComObject - it's really easy to miss, and a pain to locate the culprit.

I use a generic IDisposable wrapper class which can be used on any COM object. It works like a charm, and it keeps everything nice and clean. I can even reuse private fields (e.g. this.worksheet). It also auto-releases the object when something throws an error, due to the nature of IDisposable (the Dispose method runs as a finally).

using Microsoft.Office.Interop.Excel;

public class ExcelService
{
    private _Worksheet worksheet;

    private class ComObject<TType> : IDisposable
    {
        public TType Instance { get; set; }

        public ComObject(TType instance)
        {
            this.Instance = instance;
        }

        public void Dispose()
        {
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(this.Instance);
        }
    }

    public void CreateExcelFile(string fullFilePath)
    {
        using (var comApplication = new ComObject<Application>(new Application()))
        {
            var excelInstance = comApplication.Instance;
            excelInstance.Visible = false;
            excelInstance.DisplayAlerts = false;

            try
            {
                using (var workbooks = new ComObject<Workbooks>(excelInstance.Workbooks))
                using (var workbook = new ComObject<_Workbook>(workbooks.Instance.Add()))
                using (var comSheets = new ComObject<Sheets>(workbook.Instance.Sheets))
                {
                    using (var comSheet = new ComObject<_Worksheet>(comSheets.Instance["Sheet1"]))
                    {
                        this.worksheet = comSheet.Instance;
                        this.worksheet.Name = "Action";
                        this.worksheet.Visible = XlSheetVisibility.xlSheetHidden;
                    }

                    using (var comSheet = new ComObject<_Worksheet>(comSheets.Instance["Sheet2"]))
                    {
                        this.worksheet = comSheet.Instance;
                        this.worksheet.Name = "Status";
                        this.worksheet.Visible = XlSheetVisibility.xlSheetHidden;
                    }

                    using (var comSheet = new ComObject<_Worksheet>(comSheets.Instance["Sheet3"]))
                    {
                        this.worksheet = comSheet.Instance;
                        this.worksheet.Name = "ItemPrices";
                        this.worksheet.Activate();

                        using (var comRange = new ComObject<Range>(this.worksheet.Range["A4"]))
                        using (var comWindow = new ComObject<Window>(excelInstance.ActiveWindow))
                        {
                            comRange.Instance.Select();
                            comWindow.Instance.FreezePanes = true;
                        }
                    }

                    if (this.fullFilePath != null)
                    {
                        var currentWorkbook = (workbook.Instance as _Workbook);
                        currentWorkbook.SaveAs(this.fullFilePath, XlFileFormat.xlWorkbookNormal);
                        currentWorkbook.Close(false);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
                throw;
            }
            finally
            {
                // Close Excel instance
                excelInstance.Quit();
            }
        }
    }
}
Riegardt Steyn
  • 5,431
  • 2
  • 34
  • 49
2

Alternatively, you can kill the Excel process as explained here.

First, import SendMessage function:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

Then, send the WM_CLOSE message to the main window:

SendMessage((IntPtr)excel.Hwnd, 0x10, IntPtr.Zero, IntPtr.Zero);
2

@Denis Molodtsov in an attempt to be helpful suggested killing all processes named 'EXCEL'. That seems to be asking for trouble. There are already many answers that describe ways to get the process to stop after the call to excel.quit() by playing nice with COM interop. This is best if you can make it work.

@Kevin Vuilleumier had a great suggestion to send WM_CLOSE to the Excel window. I plan to test this.

If for some reason you need to kill an Excel App Object's Excel process, you can target it specifically using something like this:

  using System.Diagnostics;
  using System.Runtime.InteropServices;

// . . .

    [DllImport("user32.dll", SetLastError=true)]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);

// . . .

    uint excelAppPid;
    uint tid = GetWindowThreadProcessId(excel.Hwnd, out excelAppPid);

    if (tid)
    {
      Process excelAppProc = Process.GetProcessById($excelPid);
      if (excelAppProc)
      {
        excelAppProc.Kill();
      }
    }

I don't have time to fully test in C#, but I ran a quick test in Powershell where I'm having a problem with Excel not terminating and this approach works.

It's pretty straightforward. Excel App object's Hwnd property is the Excel process's hidden window handle. Pass excel.Hwnd to GetWindowThreadProcessId to get the process ID. Use that to open the process, finally invoke Kill().

At least we're sure we're killing the right process. Well, pretty sure. If the Excel process already terminated normally, it's process ID could be reused by a new process. To limit this possibility, it's important not to wait between calling excel.quit() and attempting to kill.

jimhark
  • 4,938
  • 2
  • 27
  • 28
2

Cannot close Excel.exe after Interop process

Don't make this too complicated!! Just create a simple method and call that method as follows :

// to kill the EXCELsheet file process from process Bar
private void KillSpecificExcelFileProcess() {
  foreach (Process clsProcess in Process.GetProcesses())
    if (clsProcess.ProcessName.Equals("EXCEL"))  //Process Excel?
      clsProcess.Kill();
    }
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
Jarus Rev
  • 21
  • 3
  • 1
    But later when you open Excel you will get that side bar warning about possible lost data due to the application closing unexpectedly. – Andrew Apr 24 '21 at 02:21
1

In case you are desperate. Do not use this approach unless you understand what it does:

foreach (Process proc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
{
  proc.Kill();
}

Note: This kill every process named "EXCEL".

I had to do it becase even though I've closed every single COM object in my code I still had stubborn Excel.exe process just hanging there. This is by no means the best solution, of course.

jimhark
  • 4,938
  • 2
  • 27
  • 28
Denis Molodtsov
  • 802
  • 12
  • 27
  • 8
    That's what I want to avoid, because this will close even regular Excel opened manually by the user. – provençal le breton Jun 04 '15 at 13:19
  • As this will kill any random instance of Excel that happens to be running, it's usefulness if VERY limited. I'll post a less dangerous, more targeted way to kill the Excel process used by the Excel App Object. – jimhark Oct 10 '18 at 20:45
  • It's quite possible that we want to kill all Excel processes. Thank you for adding one more disclaimer that says that we are killing every process named Excel. Apparently it is not obvious that code foreach (process in Processes){process.Kill()} kills each process. Sad times... – Denis Molodtsov Oct 15 '18 at 23:34
  • 1
    @DenisMolodtsov, I hear you. Yes, what the code does is clear to you and me, but there are a broad spectrum of users on Stack Overflow, including beginners and some who wouldn't even quality as that. If you feel your answer is better without my edit, feel free to remove it. – jimhark Oct 17 '18 at 15:48
0

I had same issue , we can solve the issue without any killing, we always forget to close interfaces which we have used form Microsoft.Office.Interop.Excel class so here is the code snippet and follow the structure and way have cleared objects , also keep an eye on Sheets interface in your code this is the main culprit we often close the application,Workbook,workbooks,range,sheet but we forget or unknowingly dont release the Sheets object or used interface so here is the code :

               Microsoft.Office.Interop.Excel.Application app = null;
        Microsoft.Office.Interop.Excel.Workbooks books = null;
        Workbook book = null;
        Sheets sheets = null;
        Worksheet sheet = null;
        Range range = null;

        try
        {
            app = new Microsoft.Office.Interop.Excel.Application();
            books = app.Workbooks;
            book = books.Add();
            sheets = book.Sheets;
            sheet = sheets.Add();
            range = sheet.Range["A1"];
            range.Value = "Lorem Ipsum";
            book.SaveAs(@"C:\Temp\ExcelBook" + DateTime.Now.Millisecond + ".xlsx");
            book.Close();
            app.Quit();
        }
        finally
        {
            if (range != null) Marshal.ReleaseComObject(range);
            if (sheet != null) Marshal.ReleaseComObject(sheet);
            if (sheets != null) Marshal.ReleaseComObject(sheets);
            if (book != null) Marshal.ReleaseComObject(book);
            if (books != null) Marshal.ReleaseComObject(books);
            if (app != null) Marshal.ReleaseComObject(app);
        }
kki
  • 1
  • 2
0

After doing several tests on my own, checking different answers, this is the shortest code that makes the process go away just a few seconds later:

var excelApp = new Microsoft.Office.Interop.Excel.Application();
var workbooks = excelApp.Workbooks;
try
{
    var wb = workbooks.Open(filePath);

    // Use worksheet, etc.
    Worksheet sheet = wb.Worksheets.get_Item(1);
}
finally
{
    excelApp.Quit();
    Marshal.ReleaseComObject(workbooks);
    Marshal.ReleaseComObject(excelApp);
}

Despite the messages about the double-dot myth, in my own tests, if I don't have a variable for Workbooks, the process would stay forever. It seems that indeed calling excelApp.Workbooks creates some objects in memory which prevent the Garbage Collector from disposing excel.exe. This means that this leaves the process in memory:

try
{
    // Do not
    var wb = excelApp.Workbooks.Open("");
}
finally
{
    excelApp.Quit();
    // Do not
    Marshal.ReleaseComObject(excelApp.Workbooks);
    Marshal.ReleaseComObject(excelApp);
}
Andrew
  • 7,602
  • 2
  • 34
  • 42
0

This code worked on me.

        Excel.Application excelApp = null;
        Excel.Workbooks excelWorkbooks = null;
        Excel.Workbook excelWorkbook = null;
        Excel._Worksheet xlWorkSheet = null;
        Excel.Range range = null;

        excelApp = new Excel.Application();
        excelWorkbooks = excelApp.Workbooks;
        excelWorkbook = excelWorkbooks.Open(excelName);
        xlWorkSheet = (Excel.Worksheet)excelWorkbook.ActiveSheet;

        range = xlWorkSheet.Range["C3"] ;
        range.Value = "Update Data";
        Marshal.ReleaseComObject(range);

        xlWorkSheet.SaveAs(path);
            
        Marshal.ReleaseComObject(xlWorkSheet);
        excelWorkbook.Close();
        Marshal.ReleaseComObject(excelWorkbook);
        excelWorkbooks.Close();
        Marshal.ReleaseComObject(excelWorkbooks);
        excelApp.Quit();
        Marshal.ReleaseComObject(excelApp);
onoffon
  • 93
  • 10
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 09 '22 at 13:29
0

Inspired by @jimhark solution: In my program, I have to open simultaneously multiple Excel files. Therefore I had to tidy up some codes.

public void DoSomeExcelWork()
{ 
    OpenExcelFile(filePath, out Application excelApp, out Workbook workbook, out Process process);    

    // do some work on your excel.

    DisposeExcelFile(excelApp, workbook, process, false);
}

This is where the excel file gets opened.

private static void OpenExcelFile(string excelFilePath, out Application excelApp, out Workbook workbook, out Process process)
{
    excelApp = new Application();
    workbook = excelApp.Workbooks.Open(excelFilePath);
    process = ProcessUtility.GetExcelProcess(excelApp);
}

You don't need to call Marshal stuff, since the process gets killed directly.

private static void DisposeExcelFile(Application excelApp, Workbook workbook, Process process, bool shouldSave = true)
{
    if (shouldSave)
        workbook.Save();

    excelApp.Application.Quit();
    process.Kill();      
}

This is where @jimhark 's solution comes in.

public static class ProcessUtility
{
    [DllImport("user32.dll")]
    static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId);

    public static Process GetExcelProcess(Microsoft.Office.Interop.Excel.Application excelApp)
    {
        int id;
        GetWindowThreadProcessId(excelApp.Hwnd, out id);
        return Process.GetProcessById(id);
    }
}
tataelm
  • 679
  • 8
  • 17
0

I have been plagued with this issue for years and finally came up with a good solution that should work for all use cases that I can think of. Whether you want your application to close the process after generating and saving or waiting until the user closes the window, along with ability to have multiple excel instances and never having the process linger.

Create this class:

using System;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;

namespace YourNameSpace
{
    public class MicrosoftApplications
    {
        [DllImport("user32.dll")]
        static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId);
        public class Excel
        {   
            public Excel()
            {
                Application = new Microsoft.Office.Interop.Excel.Application();
                RegisterExitEvent();
            }

            public Microsoft.Office.Interop.Excel.Application Application;
            
            private void RegisterExitEvent()
            {
                Application.WindowDeactivate -= XlApp_WindowDeactivate;
                Application.WindowDeactivate += XlApp_WindowDeactivate;
            }

            private void XlApp_WindowDeactivate(Workbook Wb, Window Wn)
            {
                Kill();
            }

            public void Kill()
            {
                int pid = 0;
                GetWindowThreadProcessId(Application.Hwnd, out pid);
                if (pid > 0)
                {
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(pid);
                    p.Kill();
                }
                Application = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }
}

And you can call it by: YourNameSpace.MicrosoftApplications.Excel xlApp = new YourNameSpace.MicrosoftApplications.Excel();

Do whatever you need to do by calling xlApp.Application.whatever instead of xlApp.whatever and if the user exits the excel window(s) it will kill the process(es) that were used in the code. If you want to just generate a report behind the scenes but not display the form, then simply call xlApp.Kill(); to end that specific process.

Hope this helps someone, wish I knew this about 10 years ago.

-1

İf you are handling it in one button u guys can get lastest process which you created and you can kill it.I used it.Have a good days.

//Exporting excel codes are here

System.Diagnostics.Process [] proc = System.Diagnostics.Process.GetProcessesByName("excel"); proc[proc.Length-1].Kill();

Stenquery
  • 1
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 22 '22 at 10:24