How to remove excel sheets from excel book in c #?

Let's say I have one book that has 6 sheets like Sheet1, Sheet2, Sheet3, Sheet4, Sheet5, Sheet6.

So I want to remove Sheet1, Sheet2, Sheet3. Please help me how to remove?

+6
source share
5 answers

You can call . Delete () method , for example:

Globals.Sheet1.Delete(); 

Update according to your comment:

 Excel._Worksheet ws = (Excel._Worksheet)app.Workbooks[i].Worksheets[j]; ws.Delete(); 
+3
source

I hope this code helps you:

 app.DisplayAlerts = false; worksheet.Delete(); app.Displayalerts = true; 

where the application is your XlsApplication.

+2
source

MSDN knows:

 ((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[4]).Delete(); 

or

 Globals.Sheet1.Delete(); 

See here: Practical Guide. Removing worksheets from workbooks

0
source

Referring to your example ( How to combine two excel books into one workbook in C #? ), Where the application is your Excel.Application type variable, you should write something like this:

 for (int i = 1; i <= 3; i++) ((Excel.Worksheet)app.ActiveWorkbook.Sheets[1]).Delete(); 

PS: it seems to me that the Worksheets collection is indexed starting at 1, not 0, but I'm not sure and can't check right now. If it does not work, try with values ​​for i from 0 to 2.

0
source
 Dim s1 as Excel.Worksheet s1 = wb.Sheets("Sheet1") s1.Visible = Excel.XlSheetVisibility.xlSheetVisible s1.Delete() 

You need to change the visibility because you cannot delete the sheet with the visibility set in Excel.XlSheetVisibility.xlSheetVeryHidden

0
source

Source: https://habr.com/ru/post/897231/


All Articles