C# .Net Excel Interop 删除工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/678736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:07:02  来源:igfitidea点击:

.Net Excel Interop Deleting a worksheet

c#.netexcelinterop

提问by Melursus

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).

我正在尝试使用互操作 Excel 类(对于 excel 2003)从 .Net c# 3.5 应用程序的 excel 文档中删除工作表。

I try many things like :

我尝试了很多事情,比如:

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

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

它不起作用,也不会抛出任何错误......

采纳答案by Melursus

After more than one hour looking I found the answer:

经过一个多小时的查找,我找到了答案:

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

回答by Richard Morgan

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.

同样重要的是要注意工作簿必须至少包含一张工作表;这意味着您不能删除工作簿中的所有工作表。

回答by itsho

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:

    // 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 willthrow exception when you only got one worksheet left:

     MyWorkBook.Worksheets[1].Delete();
    
  1. Excel 互操作从 1 开始计数(而不是从 0 开始),因此,删除第二个项目将导致第三个项目取而代之!。因此,删除工作表的正确方法是从最后一个到第一个

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

    或者,您可以删除列表中的第二项 ([2])两次,这会给您相同的结果。

  2. 下面的行抛出异常,当你只得到了一个工作表左:

     MyWorkBook.Worksheets[1].Delete();
    

回答by Saranya

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

回答by 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();

回答by Ismayil S

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

我们从 ac# 控制台应用程序中删除 excel 工作表,如下所示:

 Microsoft.Office.Interop.Excel.Worksheet worksheet = 
 (Worksheet)workbook.Worksheets["Worksheet_Name" (or) "Countings"];
 worksheet.Delete();

回答by Vinoth

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