如何在C#中从Excel工作簿中删除工作表?
假设我有一个工作簿,其中有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以调用 .Delete() 方法,如下所示:
Update,根据你的评论:
You can call the .Delete() method, like that:
Update, according your comment:
我希望这段代码能够帮助您:
其中 app 是您的 XlsApplication。
I hope this code will help you:
where app is your XlsApplication.
MSDN 知道:
或
参见此处:如何:删除工作簿中的工作表
MSDN knows:
or
See here: How to: Delete Worksheets from Workbooks
参考您的示例(如何在c#中将两个Excel工作簿合并为一个工作簿?),其中app是Excel.Application类型的变量,你应该写一些东西像这样:
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:
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.
您需要更改可见性,因为无法删除可见性设置为 Excel.XlSheetVisibility.xlSheetVeryHidden 的工作表
You need to change visibility because you can't delete a sheet with the visibility set to Excel.XlSheetVisibility.xlSheetVeryHidden