以编程方式删除excel工作表 [英] delete excel worksheets programmatically

查看:108
本文介绍了以编程方式删除excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有许多表格的excel工作簿。我想删除除三个之外的所有表格。

have an excel workbook with many, many sheets. I want to delete all the sheets except for three of them.

具体来说,我想知道是否有办法使用表单名称而不是ordinals删除工作表(表号)。

specifically, i would like to know if there is a way to remove the sheets using sheet name instead of ordinals (sheet number).

我正在使用excel interop和C#来处理Excel。

i am using 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;


推荐答案

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;

这篇关于以编程方式删除excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆