.Net Excel Interop Deleting a worksheet

寵の児 提交于 2019-11-27 09:24:30
Melursus

After more than one hour looking I found the answer:

xlApp.DisplayAlerts = false;
worksheet.Delete();
xlApp.DisplayAlerts = true;
itsho

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();
    

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.

Saranya
Microsoft.Office.Interop.Excel.Worksheet worksheet = Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets[1];
worksheet.Delete();
Gev Dale

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();

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();
            }
        }
Ismayil S

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();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!