Skip to content

Commit 5c2983e

Browse files
accept a collection of paths
1 parent 66c0fee commit 5c2983e

File tree

1 file changed

+55
-44
lines changed

1 file changed

+55
-44
lines changed

Public/ConvertTo-ExcelXlsx.ps1

+55-44
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,75 @@ function ConvertTo-ExcelXlsx {
33
param
44
(
55
[parameter(Mandatory = $true, ValueFromPipeline)]
6-
[string]$Path,
6+
[string[]]$Path,
77
[parameter(Mandatory = $false)]
88
[switch]$Force
99
)
1010
process {
11-
if (-Not ($Path | Test-Path) ) {
12-
throw "File not found"
13-
}
14-
if (-Not ($Path | Test-Path -PathType Leaf) ) {
15-
throw "Folder paths are not allowed"
16-
}
11+
try {
12+
foreach ($singlePath in $Path) {
13+
if (-Not ($singlePath | Test-Path) ) {
14+
throw "File not found"
15+
}
16+
if (-Not ($singlePath | Test-Path -PathType Leaf) ) {
17+
throw "Folder paths are not allowed"
18+
}
1719

18-
$xlFixedFormat = 51 #Constant for XLSX Workbook
19-
$xlsFile = Get-Item -Path $Path
20-
$xlsxPath = "{0}x" -f $xlsFile.FullName
20+
$xlFixedFormat = 51 #Constant for XLSX Workbook
21+
$xlsFile = Get-Item -Path $singlePath
22+
$xlsxPath = "{0}x" -f $xlsFile.FullName
2123

22-
if ($xlsFile.Extension -ne ".xls") {
23-
throw "Expected .xls extension"
24-
}
25-
26-
if (Test-Path -Path $xlsxPath) {
27-
if ($Force) {
28-
try {
29-
Remove-Item $xlsxPath -Force
24+
if ($xlsFile.Extension -ne ".xls") {
25+
throw "Expected .xls extension"
3026
}
31-
catch {
32-
throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $xlsxPath
27+
28+
if (Test-Path -Path $xlsxPath) {
29+
if ($Force) {
30+
try {
31+
Remove-Item $xlsxPath -Force
32+
}
33+
catch {
34+
throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $xlsxPath
35+
}
36+
Write-Verbose $("Removed {0}" -f $xlsxPath)
37+
}
38+
else {
39+
throw "{0} already exists!" -f $xlsxPath
40+
}
3341
}
34-
Write-Verbose $("Removed {0}" -f $xlsxPath)
35-
}
36-
else {
37-
throw "{0} already exists!" -f $xlsxPath
38-
}
39-
}
4042

41-
try {
42-
$Excel = New-Object -ComObject "Excel.Application"
43-
}
44-
catch {
45-
throw "Could not create Excel.Application ComObject. Please verify that Excel is installed."
46-
}
43+
if ($null -eq $Excel)
44+
{
45+
try {
46+
$Excel = New-Object -ComObject "Excel.Application"
47+
}
48+
catch {
49+
throw "Could not create Excel.Application ComObject. Please verify that Excel is installed."
50+
}
51+
}
4752

48-
try {
49-
$Excel.Visible = $false
50-
$workbook = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
51-
if ($null -eq $workbook) {
52-
Write-Host "Failed to open workbook"
53-
} else {
54-
$workbook.SaveAs($xlsxPath, $xlFixedFormat)
53+
try {
54+
$Excel.Visible = $false
55+
$workbook = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
56+
if ($null -eq $workbook) {
57+
Write-Host "Failed to open workbook"
58+
} else {
59+
$workbook.SaveAs($xlsxPath, $xlFixedFormat)
60+
}
61+
}
62+
catch {
63+
throw "Failed to convert {0} to XLSX. Error: {1}" -f $xlsFile.FullName, $_
64+
}
65+
finally {
66+
if ($null -ne $workbook) {
67+
$workbook.Close()
68+
}
69+
}
5570
}
5671
}
5772
finally {
58-
if ($null -ne $workbook) {
59-
$workbook.Close()
60-
}
61-
6273
$Excel.Quit()
74+
$Excel = $null
6375
}
6476
}
6577
}
66-

0 commit comments

Comments
 (0)