@@ -3,64 +3,75 @@ function ConvertTo-ExcelXlsx {
3
3
param
4
4
(
5
5
[parameter (Mandatory = $true , ValueFromPipeline )]
6
- [string ]$Path ,
6
+ [string [] ]$Path ,
7
7
[parameter (Mandatory = $false )]
8
8
[switch ]$Force
9
9
)
10
10
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
+ }
17
19
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
21
23
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"
30
26
}
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
+ }
33
41
}
34
- Write-Verbose $ (" Removed {0}" -f $xlsxPath )
35
- }
36
- else {
37
- throw " {0} already exists!" -f $xlsxPath
38
- }
39
- }
40
42
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
+ }
47
52
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
+ }
55
70
}
56
71
}
57
72
finally {
58
- if ($null -ne $workbook ) {
59
- $workbook.Close ()
60
- }
61
-
62
73
$Excel.Quit ()
74
+ $Excel = $null
63
75
}
64
76
}
65
77
}
66
-
0 commit comments