Powershell脚本第一次出错,但在第二次尝试时仍有效
我有一个powershell脚本,可以搜索xls文件并将其转换为xlsx。每次我第一次运行它时,我都会得到错误Powershell脚本第一次出错,但在第二次尝试时仍有效,powershell,Powershell,我有一个powershell脚本,可以搜索xls文件并将其转换为xlsx。每次我第一次运行它时,我都会得到错误 Unable to find type [Microsoft.Office.Interop.Excel.XlFileFormat]. At C:\Users\wpauling\Documents\Quarter Report Scripts\convert.ps1:2 char:18 + ... xlFixedFormat = [Microsoft.Office.Interop.Exc
Unable to find type [Microsoft.Office.Interop.Excel.XlFileFormat].
At C:\Users\wpauling\Documents\Quarter Report Scripts\convert.ps1:2 char:18
+ ... xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpen ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Microsoft.Offic...el.XlFileFormat:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
然而,当我第二次运行它时,一切都如预期的那样工作,我发疯的问题是什么。这是我的剧本
#Converts xls into xlsx
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook
write-host $xlFixedFormat
$excel = New-Object -ComObject excel.application
$excel.visible = $false
$folderpath = "C:\Users\user1\Documents\Q4"
$filetype ="*xls"
Get-ChildItem -Path $folderpath -Include $filetype -recurse |
ForEach-Object `
{
$path = ($_.fullname).substring(0, ($_.FullName).lastindexOf("."))
"Converting $path"
$workbook = $excel.workbooks.open($_.fullname)
$path += ".xlsx"
$workbook.saveas($path, $xlFixedFormat)
$workbook.close()
remove-item $_.fullname
}
$excel.Quit()
$excel = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
以行开始脚本
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
希望有帮助
p、 当然,描述性要小得多,但您也可以对变量使用数字枚举值51
,而不使用Add Type
请参阅:以行开始脚本
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
希望有帮助
p、 当然,描述性要小得多,但您也可以对变量使用数字枚举值51
,而不使用Add Type
见: