因工作需要需把excel表大量轉存成pdf
手工逐個檔案轉存並命名實在耗時
請問有沒有方法能將excel自動轉存成pdf並不重複命名呢?
例如:將一個excel表另存成100個pdf檔名不重複
感謝幫忙
mr.shadow87 wrote:
將一個excel表另存成10個pdf檔名不重複
真的可用,至於PowerShell腳本怎麼執行請上網找

問
用PowerShell轉換一個Excel檔至100個不同檔名的pdf檔
ChatGPT答
$ExcelFile = "C:\path\to\yourfile.xlsx"
$PDFDirectory = "C:\path\to\pdf\directory"
# Load the Excel Interop Assembly
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Excel") | Out-Null
# Open the Excel File
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open($ExcelFile)
# Loop through each worksheet in the workbook
for ($i=1; $i -le $Workbook.Sheets.Count; $i++) {
$Sheet = $Workbook.Sheets.Item($i)
for ($j = 1; $j -le 100; $j++) {
$PDFName = "$($PDFDirectory)\$($Sheet.Name)_$j.pdf"
# Export the current sheet to PDF
$Sheet.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixedFormatType]::xlTypePDF, $PDFName)
}
}
# Close Excel and release the Interop objects
$Workbook.Close()
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
mr.shadow87 wrote:
因工作需要需把exc...(恕刪)
嘗試使用powershell執行後出現以下錯誤
不曉得是哪邊出錯了呢?
Exception from HRESULT: 0x800A03EC
位於 C:\Users\AKAA-80008\Desktop\test.ps1:19 字元:5
+ $Sheet.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException