[英]delete blank rows on an excel worksheet, in a different excel workbook, using vba
[英]Delete worksheet from excel workbook using powershell
下面是我創建圖形並將圖形保存為圖像的代碼
$excel = New-Object -ComObject excel.application
$outputXLSX = "C:\report_10_02.xlsx"
$data = Import-Excel -Path $outputXLSX
$cd = New-ExcelChartDefinition -XRange Name -YRange Count -ChartType ColumnStacked3D -Height 300 -Title "Latency Count" -Width 1000 -SeriesHeader Count
$data | Export-Excel $outputXLSX -ExcelChartDefinition $cd -AutoNameRange -WorksheetName "Sheet2"
$macros_wb = $excel.Workbooks.open($outputXLSX)
$chart_worksheets = @("Sheet2")
$OutputType = "JPG"
foreach ($item in $chart_worksheets)
{
$macros_ws = $macros_wb.WorkSheets.item($item)
$macros_ws.activate()
$excelchart = $macros_ws.ChartObjects(1)
$Excel.Goto($excelchart.TopLeftCell,$true)
$ImagePath = "C:\Imagee.jpg"
if ($excelchart.Chart.Export($ImagePath, $OutputType)) #Export returns true/false for success/failure
{Write-Output "Exported $ImagePath"}
else
{Write-Output "Failure Exporting $ImagePath"}
}
$WorkSheet = $macros_wb.sheets.item($chart_worksheets)
#Deleting the worksheet
$WorkSheet.Delete()
#Saving the worksheet
$macros_wb.Save()
$macros_wb.close($true)
$excel.Quit()
excel 工作簿有 2 個工作表,我想在其中刪除Sheet2 。 我試過Delete()但它沒有刪除工作表。
請讓我知道這里出了什么問題
如果您已經安裝了 ImportExcel 模塊,我不確定為什么要使用ComObject
,它不需要使用ComObject
進行操作。 以下是如何從 Excel 文件中刪除工作表:
$path = 'path/to/excelfile.xlsx'
$workSheetToRemove = 'worksheetName'
Remove-Worksheet -WorksheetName $workSheetToRemove -FullName $path
如果您不確定要刪除的工作表的名稱是什么,請使用:
Get-ExcelFileSummary $path
好的,您有一個 Excel 文件,其中包含您要導出為 JPG 文件的圖表,然后刪除包含該圖表的工作表。
使用 COM 對象,您可以這樣做:
$outputXLSX = "C:\report_10_02.xlsx"
$chart_worksheet = "Sheet2"
$ImagePath = "C:\Imagee.jpg"
$OutputType = "JPG"
$excel = New-Object -ComObject excel.application
$excel.DisplayAlerts = $false
$macros_wb = $excel.Workbooks.open($outputXLSX)
$macros_ws = $macros_wb.WorkSheets.item($chart_worksheet)
$macros_ws.activate()
$excelchart = $macros_ws.ChartObjects(1)
$excel.Goto($excelchart.TopLeftCell,$true)
if ($excelchart.Chart.Export($ImagePath, $OutputType)) {Write-Host "Exported $ImagePath"}
else {Write-Warning "Failure Exporting $ImagePath"}
$macros_ws.Delete()
#Saving the worksheet
$macros_wb.Save()
$macros_wb.Close($true)
$excel.Quit()
# important, remove the used COM objects from memory
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($macros_ws)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($macros_wb)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.