31

I'm trying to delete a worksheet from a excel document from a .Net c# 3.5 application with the interop Excel class (for excel 2003).

I try many things like :

Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
worksheet.Delete();

It's doesn't work and doesn't throw any error ...

Melursus
  • 10,328
  • 19
  • 69
  • 103

7 Answers7

74

After more than one hour looking I found the answer:

xlApp.DisplayAlerts = false;
worksheet.Delete();
xlApp.DisplayAlerts = true;
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Melursus
  • 10,328
  • 19
  • 69
  • 103
  • I am too used to working on web apps! I forgot that alert suppression is sometimes required when working on web forms, and its quite common :) – hatsrumandcode Apr 14 '16 at 12:56
  • 3
    This has nothing to do with Forms. This is a bug in Excel. When you have set Excel.Application.Visible = false then Excel should not display any alert "When closing the worksheet you may lose data, Bla Bla" when deleting a sheet by automation. – Elmue May 23 '17 at 00:52
  • In MS Excel 2013, it generate macro like```Sheets("RAW").Select``` ```ActiveWindow.SelectedSheets.Delete``` by converting this to C# code, it didn't work, Why? – Arsalan Khan Feb 01 '20 at 06:45
15

When dealing with deleting Excel Worksheets, there are two important things to know:

  1. Excel interop counts from 1 (and not from zero), therefore, removing the second item will cause the third item to take its place!. so, the proper way to remove worksheets is from the last to the first:

    // Remove LAST worksheet
    MyWorkBook.Worksheets[3].Delete();
    
    // and only then remove the second (which is the last one)
    MyWorkBook.Worksheets[2].Delete();
    

    alternatively, you can delete the second item ([2]) on the list twice, which will give you the same result.

  2. The following line will throw exception when you only got one worksheet left:

     MyWorkBook.Worksheets[1].Delete();
    
Elmue
  • 7,602
  • 3
  • 47
  • 57
itsho
  • 4,640
  • 3
  • 46
  • 68
  • Thanks - you saved me hours of debugging. "if you remove the Second Worksheet, the third one will take her place.." this is so obvious but so easy too miss. – digitguy Sep 18 '15 at 05:12
  • 2
    Your answer does not answer the question. If the index would be wrong you would get an exception. But Melursus wrote that he does NOT get any exception. The correct answer is from Melursus himself. See below. – Elmue May 23 '17 at 00:39
9

It is also important to note that the workbook must contain at least one worksheet; this means you cannot delete all worksheets in a workbook.

Richard Morgan
  • 7,601
  • 9
  • 49
  • 86
  • Yes, that is right, and if you attempt to delete them all, you will get a COMException. – Chris Oct 22 '09 at 19:26
2
Microsoft.Office.Interop.Excel.Worksheet worksheet = Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets[1];
worksheet.Delete();
Brandon
  • 68,708
  • 30
  • 194
  • 223
Saranya
  • 31
  • 1
0

Try to find worksheet by name:

var app = new Microsoft.Office.Interop.Excel.Application();
var workbook = app.Workbooks.Add();
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets["Sheet3"]).Delete();
slavoo
  • 5,798
  • 64
  • 37
  • 39
0

we can delete the work sheet like this

 Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

                if (xlApp == null)
                {

                    return;
                }


                xlApp.DisplayAlerts = false;
                string filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
                                        + "\\Sample.xlsx";
                Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
                Excel.Sheets worksheets = xlWorkBook.Worksheets;

                worksheets[4].Delete();
                worksheets[3].Delete();
                xlWorkBook.Save();
                xlWorkBook.Close();

                releaseObject(worksheets);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);

and use this

  static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                throw ex;

            }
            finally
            {
                GC.Collect();
            }
        }
Vinoth
  • 972
  • 1
  • 16
  • 47
-2

We delete excel worksheets from a c# console application like this:

 Microsoft.Office.Interop.Excel.Worksheet worksheet = 
 (Worksheet)workbook.Worksheets["Worksheet_Name" (or) "Countings"];
 worksheet.Delete();
War
  • 8,539
  • 4
  • 46
  • 98
Ismayil S
  • 223
  • 3
  • 20