如何使用powershell在excel文件中删除工作表?

如何使用powershell在excel文件中删除工作表?,powershell,Powershell,这是我的代码,如下所示: # Specify the path to the Excel file and the WorkSheet Name $FilePath = "C:\Downloads\Portalroom_CW30.xlsx" $SheetName = "4_docexchange" # Create an Object Excel.Application using Com interface $objExcel = New-Object -ComObject Excel.App

这是我的代码,如下所示:

# Specify the path to the Excel file and the WorkSheet Name
$FilePath = "C:\Downloads\Portalroom_CW30.xlsx"
$SheetName = "4_docexchange"
# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet 'BuildSpecs'
$WorkSheet = $WorkBook.sheets.item($SheetName)
$WorkBook.close($true)
$objExcel.Quit()

请帮我解决,谢谢

您可以在工作表上调用
Delete()
方法来执行此操作。但在此之前,您希望将
显示警报
选项设置为
$false
,并在完成删除后保存
$Workbook
。您的代码将如下所示-

#Specify the path to the Excel file and the WorkSheet Name
$FilePath = "C:\Downloads\Portalroom_CW30.xlsx"
$SheetName = "4_docexchange"
#Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
#Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false

#Set Display alerts as false
$objExcel.displayalerts = $False

#Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
#Load the WorkSheet 'BuildSpecs'
$WorkSheet = $WorkBook.sheets.item($SheetName)

#Deleting the worksheet
$WorkSheet.Delete()
#Saving the worksheet
$WorkBook.Save()
$WorkBook.close($true)
$objExcel.Quit()

为此,您可以在工作表上调用
Delete()
方法。但在此之前,您希望将
显示警报
选项设置为
$false
,并在完成删除后保存
$Workbook
。您的代码将如下所示-

#Specify the path to the Excel file and the WorkSheet Name
$FilePath = "C:\Downloads\Portalroom_CW30.xlsx"
$SheetName = "4_docexchange"
#Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
#Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false

#Set Display alerts as false
$objExcel.displayalerts = $False

#Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
#Load the WorkSheet 'BuildSpecs'
$WorkSheet = $WorkBook.sheets.item($SheetName)

#Deleting the worksheet
$WorkSheet.Delete()
#Saving the worksheet
$WorkBook.Save()
$WorkBook.close($true)
$objExcel.Quit()