0

Goal:

  • Copy specific sheet from workbook and paste it into a brand new excel file. (Done)

  • Close the excel program properly, so the process does not appear in the Task Manager. (Not-Done) - Main Issue

Code:

public Excel.Application excelApp = null;
public Excel.Workbooks workbooks = null;
public Excel.Workbook target = null;

private void button1_Click(object sender, EventArgs e)
{
    //Excel Application
    excelApp = new Excel.Application();
    //Excel Workbooks
    workbooks = excelApp.Workbooks;
    //Excel Workbook
    target = workbooks.Add(@"C:\Users\LV98\Desktop\Test C#\test.xlsx");
    //Excel all sheets from Workbook
    Excel.Sheets sheets = target.Worksheets;
    //Get specific sheet name
    Excel.Worksheet workingSheet = (Excel.Worksheet)sheets.get_Item("Sheet2");
    //New book
    var newbook = excelApp.Workbooks.Add(1);
    //Copy selected sheet to new book
    workingSheet.Copy(newbook.Sheets[1]);

    newbook.SaveAs(@"C:\Users\LV98\Desktop\Test C#\template.xlsx");
    newbook.Close(0);

    target.Close();

    excelApp.Quit();
}

Details:

It works as it should. But the original file we copied the sheet from. The Excel process is still running.

enter image description here

This means the file is locked for editing.

What I have tried:

public Excel.Application excelApp = null;
public Excel.Workbooks workbooks = null;
public Excel.Workbook target = null;

private void button1_Click(object sender, EventArgs e)
{
    //Excel Application
    excelApp = new Excel.Application();
    //Excel Workbooks
    workbooks = excelApp.Workbooks;
    //Excel Workbook
    target = workbooks.Add(@"C:\Users\LV98\Desktop\Test C#\test.xlsx");
    //Excel all sheets from Workbook
    Excel.Sheets sheets = target.Worksheets;
    //Get specific sheet name
    Excel.Worksheet workingSheet = (Excel.Worksheet)sheets.get_Item("Sheet2");
    //New book
    var newbook = excelApp.Workbooks.Add(1);
    //Copy selected sheet to new book
    workingSheet.Copy(newbook.Sheets[1]);

    newbook.SaveAs(@"C:\Users\LV98\Desktop\Test C#\template.xlsx");
    newbook.Close(0);

    target.Close();

System.Runtime.InteropServices.Marshal.ReleaseComObject(workingSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(newbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(target);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);   
}

Details:

Got this from here :https://stackoverflow.com/a/28080347/12485722

This still does not work. Works exactly the same as the 1st code, and the EXCEL.exe process is still running.

Question:

Why is it still running as a process? And how can I fix this?

Eduards
  • 1,734
  • 2
  • 12
  • 37
  • See the accepted answer here: https://stackoverflow.com/questions/25134024/clean-up-excel-interop-objects-with-idisposable/25135685#25135685 – LocEngineer Mar 13 '20 at 11:46
  • 1
    I'd suggest the answers linked would work for you if you didn't keep a reference to the Application, Workbooks and Workbook after you tell Excel to quit. These can't be garbage collected until they're no longer referenced by your window. I'd also note in your second code snippet you don't actually call `Quit`. – Charles Mager Mar 13 '20 at 11:52
  • 1
    If you have the ability to use it I can strongly recommend the [EPPlus nuget package](https://epplussoftware.com/) for Excel generation. – Rasmus Björling Mar 13 '20 at 12:52

1 Answers1

0

I have managed to find one solution for this. Which is to simply kill the process.

Got it from here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/d438c273-6876-4d2b-9f25-842838bce0b4/excel-is-still-running-though-i-quit-and-released-the-object?forum=vsto

Here is the fully working code:

public Excel.Application excelApp = null;
public Excel.Workbooks workbooks = null;
public Excel.Workbook target = null;

private void button1_Click(object sender, EventArgs e)
{
    //Excel Application
    excelApp = new Excel.Application();
    //Excel Workbooks
    workbooks = excelApp.Workbooks;
    //Excel Workbook
    target = workbooks.Add(@"C:\Users\LV98\Desktop\Test C#\test.xlsx");
    //Excel all sheets from Workbook
    Excel.Sheets sheets = target.Worksheets;
    //Get specific sheet name
    Excel.Worksheet workingSheet = (Excel.Worksheet)sheets.get_Item("Sheet2");
    //New book
    var newbook = excelApp.Workbooks.Add(1);
    //Copy selected sheet to new book
    workingSheet.Copy(newbook.Sheets[1]);

    newbook.SaveAs(@"C:\Users\LV98\Desktop\Test C#\template.xlsx");
    newbook.Close(0);

    target.Close();

    excelApp.Quit();

    killExcel();
}

        private void killExcel()
        {
            System.Diagnostics.Process[] PROC = System.Diagnostics.Process.GetProcessesByName("EXCEL");
            foreach (System.Diagnostics.Process PK in PROC)
            {
                if (PK.MainWindowTitle.Length == 0)
                {
                    PK.Kill();
                }
            }
        }
Eduards
  • 1,734
  • 2
  • 12
  • 37