Compare commits

...

3 Commits

Author SHA1 Message Date
Edward Miller
e1e4cfd02e use ChangeExtension for robustness 2024-05-21 16:15:15 -05:00
Edward Miller
1c6be383cc accept a collection of paths 2024-05-21 16:15:12 -05:00
Edward Miller
66c0fee1e9 Prevent error: "Unable to get the SaveAs property of the Workbook class" 2024-05-21 15:56:53 -05:00

View File

@@ -3,60 +3,76 @@ function ConvertTo-ExcelXlsx {
param param
( (
[parameter(Mandatory = $true, ValueFromPipeline)] [parameter(Mandatory = $true, ValueFromPipeline)]
[string]$Path, [string[]]$Path,
[parameter(Mandatory = $false)] [parameter(Mandatory = $false)]
[switch]$Force [switch]$Force
) )
process { process {
if (-Not ($Path | Test-Path) ) { try {
throw "File not found" foreach ($singlePath in $Path) {
} if (-Not ($singlePath | Test-Path) ) {
if (-Not ($Path | Test-Path -PathType Leaf) ) { throw "File not found"
throw "Folder paths are not allowed" }
} if (-Not ($singlePath | Test-Path -PathType Leaf) ) {
throw "Folder paths are not allowed"
}
$xlFixedFormat = 51 #Constant for XLSX Workbook $xlFixedFormat = 51 #Constant for XLSX Workbook
$xlsFile = Get-Item -Path $Path $xlsFile = Get-Item -Path $singlePath
$xlsxPath = "{0}x" -f $xlsFile.FullName $xlsxPath = [System.IO.Path]::ChangeExtension($xlsFile.FullName, ".xlsx")
if ($xlsFile.Extension -ne ".xls") { if ($xlsFile.Extension -ne ".xls") {
throw "Expected .xls extension" throw "Expected .xls extension"
} }
if (Test-Path -Path $xlsxPath) {
if ($Force) {
try {
Remove-Item $xlsxPath -Force
}
catch {
throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $xlsxPath
}
Write-Verbose $("Removed {0}" -f $xlsxPath)
}
else {
throw "{0} already exists!" -f $xlsxPath
}
}
if ($null -eq $Excel)
{
try {
$Excel = New-Object -ComObject "Excel.Application"
}
catch {
throw "Could not create Excel.Application ComObject. Please verify that Excel is installed."
}
}
if (Test-Path -Path $xlsxPath) {
if ($Force) {
try { try {
Remove-Item $xlsxPath -Force $Excel.Visible = $false
$workbook = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
if ($null -eq $workbook) {
Write-Host "Failed to open workbook"
} else {
$workbook.SaveAs($xlsxPath, $xlFixedFormat)
}
} }
catch { catch {
throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $xlsxPath Write-Error ("Failed to convert {0} to XLSX." -f $xlsFile.FullName)
throw
}
finally {
if ($null -ne $workbook) {
$workbook.Close()
}
} }
Write-Verbose $("Removed {0}" -f $xlsxPath)
} }
else {
throw "{0} already exists!" -f $xlsxPath
}
}
try {
$Excel = New-Object -ComObject "Excel.Application"
}
catch {
throw "Could not create Excel.Application ComObject. Please verify that Excel is installed."
}
try {
$Excel.Visible = $false
$null = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
$Excel.ActiveWorkbook.SaveAs($xlsxPath, $xlFixedFormat)
} }
finally { finally {
if ($null -ne $Excel.ActiveWorkbook) {
$Excel.ActiveWorkbook.Close()
}
$Excel.Quit() $Excel.Quit()
$Excel = $null
} }
} }
} }