Delete excel sheets programmatically

I have an Excel workbook with many sheets. I want to delete all sheets except three of them.

In particular, I would like to know if there is a way to delete sheets using the sheet name instead of ordinals (sheet number).

I use excel interop and C # to work with Excel.

Microsoft.Office.Interop.Excel.Application xlApp = null; Excel.Workbook xlWorkbook = null; Excel.Sheets xlSheets = null; Excel.Worksheet xlNewSheet = null; 
+7
source share
2 answers
 xlApp.DisplayAlerts = false; for (int i = xlApp.ActiveWorkbook.Worksheets.Count; i > 0 ; i--) { Worksheet wkSheet = (Worksheet)xlApp.ActiveWorkbook.Worksheets[i]; if (wkSheet.Name == "NameOfSheetToDelete") { wkSheet.Delete(); } } xlApp.DisplayAlerts = true; 
+10
source

I know this is old, but I just use parries

workBook.Sheets["Sheet1"].Delete();

+2
source

All Articles