mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Compare commits
4 Commits
e1e4cfd02e
...
34c30eb301
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34c30eb301 | ||
|
|
37cd7335c7 | ||
|
|
b6034c37d8 | ||
|
|
88a3aac640 |
@@ -5,7 +5,11 @@ function ConvertTo-ExcelXlsx {
|
||||
[parameter(Mandatory = $true, ValueFromPipeline)]
|
||||
[string[]]$Path,
|
||||
[parameter(Mandatory = $false)]
|
||||
[switch]$Force
|
||||
[switch]$Force,
|
||||
[parameter(Mandatory = $false)]
|
||||
[switch]$CacheToTemp,
|
||||
[parameter(Mandatory = $false)]
|
||||
[string]$CacheDirectory
|
||||
)
|
||||
process {
|
||||
try {
|
||||
@@ -19,24 +23,24 @@ function ConvertTo-ExcelXlsx {
|
||||
|
||||
$xlFixedFormat = 51 #Constant for XLSX Workbook
|
||||
$xlsFile = Get-Item -Path $singlePath
|
||||
$xlsxPath = [System.IO.Path]::ChangeExtension($xlsFile.FullName, ".xlsx")
|
||||
$destinationXlsxPath = [System.IO.Path]::ChangeExtension($xlsFile.FullName, ".xlsx")
|
||||
|
||||
if ($xlsFile.Extension -ne ".xls") {
|
||||
throw "Expected .xls extension"
|
||||
}
|
||||
|
||||
if (Test-Path -Path $xlsxPath) {
|
||||
if (Test-Path -Path $destinationXlsxPath) {
|
||||
if ($Force) {
|
||||
try {
|
||||
Remove-Item $xlsxPath -Force
|
||||
Remove-Item $destinationXlsxPath -Force
|
||||
}
|
||||
catch {
|
||||
throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $xlsxPath
|
||||
throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $destinationXlsxPath
|
||||
}
|
||||
Write-Verbose $("Removed {0}" -f $xlsxPath)
|
||||
Write-Verbose $("Removed {0}" -f $destinationXlsxPath)
|
||||
}
|
||||
else {
|
||||
throw "{0} already exists!" -f $xlsxPath
|
||||
throw "{0} already exists!" -f $destinationXlsxPath
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,13 +54,32 @@ function ConvertTo-ExcelXlsx {
|
||||
}
|
||||
}
|
||||
|
||||
if ($CacheToTemp) {
|
||||
if (-not $CacheDirectory) {
|
||||
$CacheDirectory = [System.IO.Path]::GetTempPath()
|
||||
}
|
||||
$tempPath = [System.IO.Path]::Combine($CacheDirectory, [System.IO.Path]::GetFileName($xlsFile.FullName))
|
||||
Write-Host ("Using Temp path: {0}" -f $tempPath)
|
||||
Copy-Item -Path $xlsFile.FullName -Destination $tempPath -Force
|
||||
$fileToProcess = $tempPath
|
||||
}
|
||||
else {
|
||||
$fileToProcess = $xlsFile.FullName
|
||||
}
|
||||
|
||||
$xlsxPath = [System.IO.Path]::ChangeExtension($fileToProcess, ".xlsx")
|
||||
|
||||
try {
|
||||
$Excel.Visible = $false
|
||||
$workbook = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
|
||||
$workbook = $Excel.Workbooks.Open($fileToProcess, $null, $true)
|
||||
if ($null -eq $workbook) {
|
||||
Write-Host "Failed to open workbook"
|
||||
} else {
|
||||
$workbook.SaveAs($xlsxPath, $xlFixedFormat)
|
||||
|
||||
if ($CacheToTemp) {
|
||||
Copy-Item -Path $xlsxPath -Destination $destinationXlsxPath -Force
|
||||
}
|
||||
}
|
||||
}
|
||||
catch {
|
||||
@@ -66,13 +89,23 @@ function ConvertTo-ExcelXlsx {
|
||||
finally {
|
||||
if ($null -ne $workbook) {
|
||||
$workbook.Close()
|
||||
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) | Out-Null
|
||||
$workbook = $null
|
||||
}
|
||||
|
||||
if ($CacheToTemp) {
|
||||
Remove-Item -Path $tempPath -Force
|
||||
Remove-Item -Path $xlsxPath -Force
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
$Excel.Quit()
|
||||
$Excel = $null
|
||||
if ($null -ne $Excel) {
|
||||
$Excel.Quit()
|
||||
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Excel) | Out-Null
|
||||
$Excel = $null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user