Create Convert-XLSToXLSX

This commit is contained in:
Mikkel Nordberg
2017-06-23 15:55:06 +02:00
committed by GitHub
parent 39d176e31b
commit 1b695478e7

48
Convert-XLSToXLSX Normal file
View File

@@ -0,0 +1,48 @@
Function Convert-XLSToXLSX {
[CmdletBinding()]
PARAM
(
[parameter(Mandatory=$true, ValueFromPipeline)]
[ValidateScript({
if(-Not ($_ | Test-Path) ){
throw "File not found"
}
if(-Not ($_ | Test-Path -PathType Leaf) ){
throw "Folder paths are not allowed"
}
return $true
})]
[string]$Path,
[parameter(Mandatory=$false)]
[switch]$Force
)
$xlFixedFormat = 51 #Constant for XLSX Workbook
$xlsFile = Get-Item -Path $Path
$xlsxPath = "{0}x" -f $xlsFile.FullName
if($xlsFile.Extension.ToLower() -ne ".xls"){
throw "Expected .xls extension"
}
if(Test-Path -Path $xlsxPath){
if($Force){
Remove-Item $xlsxPath -Force
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."
}
$Excel.Visible = $false
$Excel.Workbooks.Open($xlsFile.FullName) | Out-Null
$Excel.ActiveWorkbook.SaveAs($xlsxPath, $xlFixedFormat)
$Excel.ActiveWorkbook.Close()
$Excel.Quit()
}