简体   繁体   English

从 .xlsm 文件中删除工作表

[英]delete worksheet from .xlsm file

I am trying to delete a named Worksheet from an .xlsm file.我正在尝试从.xlsm文件中删除命名工作表。

I followed the example posted here but it is not working for me.我按照此处发布的示例进行操作但它对我不起作用。 When I open the .xlsm file to check whether the Worksheet has been deleted, it is still there.当我打开.xlsm文件检查工作表是否已被删除时,它仍然存在。

Here is my code:这是我的代码:

$file2 = 'c:\file.xlsm' # destination's fullpath

$xl = new-object -c excel.application
$xl.displayAlerts = $false # don't prompt the user

$wb2 = $xl.workbooks.open($file2) # open target

$sh2_wb2 = $wb2.sheets | where {$_.name -eq "myWorksheet"}
$sh2_wb2.delete() #Delete original sheet in template

$wb2.close($true) # close and save destination workbook
$xl.quit()
spps -n excel

What am I doing wrong?我究竟做错了什么?

Edit:编辑:

I changed my code to make the Excel Object visible when opening it.我更改了代码以使 Excel 对象在打开时可见。 I then noticed that the delete call is being sent, but it is asking the user to confirm whether the delete should happen: Data may exist in the sheet(s) selected for deletion. To permanently delete the data, press Delete.然后我注意到正在发送delete调用,但它要求用户确认删除是否应该发生: Data may exist in the sheet(s) selected for deletion. To permanently delete the data, press Delete. Data may exist in the sheet(s) selected for deletion. To permanently delete the data, press Delete.

I then attempted to add a few more displayAlerts = $false in my code, but it is still giving me that prompt.然后我尝试在我的代码中添加更多displayAlerts = $false ,但它仍然给我提示。

Here's my updated code, although it still does not work.这是我更新的代码,虽然它仍然不起作用。

$file2 = 'c:\file.xlsm' # destination's fullpath

$xl = new-object -com excel.application -Property @{Visible = $true}
$xl.displayAlerts = $false # don't prompt the user

$wb2 = $xl.workbooks.open($file2) # open target
$wb2.displayAlerts = $false # don't prompt the user

$sh2_wb2 = $wb2.sheets | where {$_.name -eq "myWorksheet"}
$sh2_wb2.displayAlerts = $false # don't prompt the user
$sh2_wb2.delete() #Delete original sheet in template

$wb2.close($true) # close and save destination workbook
$xl.quit()
spps -n excel

Try this, its help for me :试试这个,它对我有帮助:

$Excel = New-Object -ComObject Excel.Application
$workbook = $Excel.workbooks.add()
#working with sheets
$workbook.worksheets.item("Sheet1").Delete()

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

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