如何在C#中从Excel工作簿中删除工作表?

发布于 2024-12-04 11:25:43 字数 120 浏览 0 评论 0原文

假设我有一个工作簿,其中有 6 张工作表:Sheet1、Sheet2、Sheet3、Sheet4、Sheet5、Sheet6。

所以从这里我想删除Sheet1、Sheet2、Sheet3。我怎样才能做到这一点?

Let's say I have one workbook which is having 6 sheets as Sheet1, Sheet2, Sheet3, Sheet4, Sheet5, Sheet6.

So from here I want to delete Sheet1, Sheet2, Sheet3. How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一笑百媚生 2024-12-11 11:25:43

您可以调用 .Delete() 方法,如下所示:

Globals.Sheet1.Delete();

Update,根据你的评论:

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

You can call the .Delete() method, like that:

Globals.Sheet1.Delete();

Update, according your comment:

Excel._Worksheet ws = (Excel._Worksheet)app.Workbooks[i].Worksheets[j];
ws.Delete();
金橙橙 2024-12-11 11:25:43

我希望这段代码能够帮助您:

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

其中 app 是您的 XlsApplication。

I hope this code will help you:

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

where app is your XlsApplication.

南薇 2024-12-11 11:25:43

MSDN 知道:

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

Globals.Sheet1.Delete();

参见此处:如何:删除工作簿中的工作表

MSDN knows:

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

or

Globals.Sheet1.Delete();

See here: How to: Delete Worksheets from Workbooks

南冥有猫 2024-12-11 11:25:43

参考您的示例(如何在c#中将两个Excel工作簿合并为一个工作簿?),其中appExcel.Application类型的变量,你应该写一些东西像这样:

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

PS:它在我看来,Worksheets 集合的索引是从 1 开始的,而不是从 0 开始,但我不确定,现在无法检查。如果它不起作用,请尝试使用 0 到 2 之间的 i 值。

Referring to your example (How to merge two excel workbook into one workbook in c#?), where app is your variable of type Excel.Application, you should write something like this:

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

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

海螺姑娘 2024-12-11 11:25:43
Dim s1 as Excel.Worksheet
s1 = wb.Sheets("Sheet1")
s1.Visible = Excel.XlSheetVisibility.xlSheetVisible
s1.Delete()

您需要更改可见性,因为无法删除可见性设置为 Excel.XlSheetVisibility.xlSheetVeryHidden 的工作表

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

You need to change visibility because you can't delete a sheet with the visibility set to Excel.XlSheetVisibility.xlSheetVeryHidden

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文