Skip to content

Commit

Permalink
accept a collection of paths
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardmiller-mesirow committed May 21, 2024
1 parent 66c0fee commit 88a3aac
Showing 1 changed file with 61 additions and 44 deletions.
105 changes: 61 additions & 44 deletions Public/ConvertTo-ExcelXlsx.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,81 @@ function ConvertTo-ExcelXlsx {
param
(
[parameter(Mandatory = $true, ValueFromPipeline)]
[string]$Path,
[string[]]$Path,
[parameter(Mandatory = $false)]
[switch]$Force
)
process {
if (-Not ($Path | Test-Path) ) {
throw "File not found"
}
if (-Not ($Path | Test-Path -PathType Leaf) ) {
throw "Folder paths are not allowed"
}
try {
foreach ($singlePath in $Path) {
if (-Not ($singlePath | Test-Path) ) {
throw "File not found"
}
if (-Not ($singlePath | Test-Path -PathType Leaf) ) {
throw "Folder paths are not allowed"
}

$xlFixedFormat = 51 #Constant for XLSX Workbook
$xlsFile = Get-Item -Path $Path
$xlsxPath = "{0}x" -f $xlsFile.FullName
$xlFixedFormat = 51 #Constant for XLSX Workbook
$xlsFile = Get-Item -Path $singlePath
$xlsxPath = "{0}x" -f $xlsFile.FullName

if ($xlsFile.Extension -ne ".xls") {
throw "Expected .xls extension"
}

if (Test-Path -Path $xlsxPath) {
if ($Force) {
try {
Remove-Item $xlsxPath -Force
if ($xlsFile.Extension -ne ".xls") {
throw "Expected .xls extension"
}
catch {
throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $xlsxPath

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
}
}
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."
}
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."
}
}

try {
$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)
try {
$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 {
Write-Error ("Failed to convert {0} to XLSX." -f $xlsFile.FullName)
throw
}
finally {
if ($null -ne $workbook) {
$workbook.Close()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) | Out-Null
$workbook = $null
}
}
}
}
finally {
if ($null -ne $workbook) {
$workbook.Close()
if ($null -ne $Excel) {
$Excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Excel) | Out-Null
$Excel = $null
}

$Excel.Quit()
}
}
}

0 comments on commit 88a3aac

Please sign in to comment.