如何在 PowerShell 中将多个 excel 工作簿的第一个工作表转换为 pdf?

How to convert first worksheet of several excel workbooks into pdf in PowerShell?

这是我第一次使用 PowerShell 脚本。我想先将 sheet 的工作簿转换为 pdf。我得到了下面的代码,它将工作簿的所有作品sheets 转换为 pdf。如何更改此代码以仅将第一个作品 sheet 转换为 pdf?请帮忙

$path = "C:\Users\addns\Desktop\Template"

$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type]

$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse

$objExcel = New-Object -ComObject excel.application

$objExcel.visible = $false

foreach($wb in $excelFiles)

{

 $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + ".pdf")

 $workbook = $objExcel.workbooks.open($wb.fullname, 1)

 $workbook.Saved = $true

"saving $filepath"

 $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)

 $objExcel.Workbooks.close()

}

$objExcel.Quit()

您可以使用 $workbook.WorkSheets.Item(1) 获取 $workbook 中的第一个工作表,然后在其上调用导出函数。