简体   繁体   English

.Net Excel 互操作删除工作表

[英].Net Excel Interop Deleting a worksheet

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'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...它不起作用,也不会引发任何错误......

After more than one hour looking I found the answer:经过一个多小时的寻找,我找到了答案:

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

When dealing with deleting Excel Worksheets , there are two important things to know:在处理删除Excel Worksheets时,需要了解两件重要的事情:

  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 : Excel 互操作从 1 开始计数(而不是从零开始),因此,删除第二项将导致第三项取而代之。因此,删除工作表的正确方法是从最后一项到第一项

     // 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])两次,这将为您提供相同的结果。

  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.这意味着您不能删除工作簿中的所有工作表。

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 delete excel worksheets from a c# console application like this:我们从 c# 控制台应用程序中删除 excel 工作表,如下所示:

 Microsoft.Office.Interop.Excel.Worksheet worksheet = 
 (Worksheet)workbook.Worksheets["Worksheet_Name" (or) "Countings"];
 worksheet.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();
            }
        }
Microsoft.Office.Interop.Excel.Worksheet worksheet = Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets[1];
worksheet.Delete();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM