【问题标题】:How to convert first worksheet of several excel workbooks into pdf in PowerShell?如何在PowerShell中将几个excel工作簿的第一个工作表转换为pdf?
【发布时间】:2018-12-25 14:38:21
【问题描述】:

这是我第一次使用 PowerShell 脚本。我想将第一张工作簿转换为 pdf。我得到了下面的代码,它将工作簿的所有工作表转换为 pdf。如何更改此代码以仅将第一个工作表转换为 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()

【问题讨论】:

  • 顺便说一句:您可以将"Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type] 简化为类型文字:[Microsoft.Office.Interop.Excel.xlFixedFormatType]

标签: powershell powershell-2.0


【解决方案1】:

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

【讨论】:

  • 感谢您的回答。我将 for 循环中的 wb 更改为 $workbook.WorkSheets.Item(1) 并在导出时相同,但它不起作用。
  • 你能不能把这行$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)换成$workbook.WorkSheets.Item(1).ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)
猜你喜欢
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 2018-04-28
相关资源
最近更新 更多