good-arrow’s blog

https://good-arrow.net/

Powershell で Excel から PDF を出力

印刷設定などは Excel ファイルのまま。

$pdf = (Convert-Path .) + "\出力ファイル名.pdf"

$excelFile = (Convert-Path .) + "\ファイル.xlsx"
$sheetName = "Sheet1"

# Excelを開く
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $False
$book = $excel.Workbooks.Open($excelFile)
$sheet = $book.Worksheets.Item($sheetName)

# PDF出力
$sheet.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixedFormatType]::xlTypePDF, $pdf)

# Excelを閉じる
$excel.Quit()
$excel = $null
[GC]::Collect()