用于将同一工作簿中的多个 Excel 工作表转换为单独的 PDF 的 Powershell 脚本,以工作表标题命名。
$path = “I:\”
$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, 3)
$workbook.Saved = $true
“saving $filepath”
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)
$objExcel.Workbooks.close()
}
$objExcel.Quit()`
我找到了解决方案。这还会保存 PDF 及其特定的工作表名称。
$path = “C:\Testing”
$xlFixedFormat = “Microsoft.Office.Interop.Excel.xlFixedFormatType” -as
[type]
$excelFiles = Get-ChildItem -Path "C:\Testing\file.xlsx"
$objExcel = New-Object -ComObject excel.application
$objExcel.visible = $false
foreach($wb in $excelFiles)
{
$workbook = $objExcel.workbooks.open($wb.fullname, 3)
foreach ($sheet in $workbook.Worksheets) {
$filepath = Join-Path -Path $path -ChildPath ($sheet.BaseName)
$sheet.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath +
$sheet.Name)}
$workbook.Saved = $true
“saving $filepath”
$objExcel.Workbooks.close()
}
$objExcel.Quit()