fix: Improve Excel file handling with temporary paths and better cleanup
This commit is contained in:
parent
2077423bc0
commit
31407f0330
@ -458,41 +458,46 @@ function Convert-Files {
|
|||||||
|
|
||||||
# Convert the file to XLSX
|
# Convert the file to XLSX
|
||||||
$xlsxFileName = $originalFileName -replace "\.xls[m]?$", ".xlsx"
|
$xlsxFileName = $originalFileName -replace "\.xls[m]?$", ".xlsx"
|
||||||
# Use Join-Path to properly handle paths with spaces
|
# Before the Excel conversion, get the full paths and clean them
|
||||||
$xlsxPath = Join-Path $env:TEMP $xlsxFileName
|
$xlsxPath = Join-Path $env:TEMP $xlsxFileName
|
||||||
$downloadPath = Join-Path $env:TEMP $originalFileName
|
$downloadPath = Join-Path $env:TEMP $originalFileName
|
||||||
|
|
||||||
# Convert the paths to proper Windows paths
|
# Clean the paths and ensure they're valid
|
||||||
$xlsxPath = [System.IO.Path]::GetFullPath($xlsxPath)
|
$xlsxPath = [System.IO.Path]::GetFullPath($xlsxPath)
|
||||||
$downloadPath = [System.IO.Path]::GetFullPath($downloadPath)
|
$downloadPath = [System.IO.Path]::GetFullPath($downloadPath)
|
||||||
|
|
||||||
|
# Verify the download file exists
|
||||||
|
if (-not (Test-Path -LiteralPath $downloadPath)) {
|
||||||
|
throw "Downloaded file not found at: $downloadPath"
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
# Open Excel
|
# Open Excel
|
||||||
$excel = New-Object -ComObject Excel.Application
|
$excel = New-Object -ComObject Excel.Application
|
||||||
$excel.Visible = $false
|
$excel.Visible = $false
|
||||||
$excel.DisplayAlerts = $false # Suppress Excel alerts
|
$excel.DisplayAlerts = $false
|
||||||
|
|
||||||
try {
|
try {
|
||||||
# Convert paths to proper format for Excel
|
# Use proper path handling for Excel
|
||||||
$excelDownloadPath = $downloadPath -replace "\\", "\\"
|
$workbook = $excel.Workbooks.Open([string]$downloadPath)
|
||||||
$excelXlsxPath = $xlsxPath -replace "\\", "\\"
|
|
||||||
|
|
||||||
$workbook = $excel.Workbooks.Open($excelDownloadPath)
|
|
||||||
|
|
||||||
# Save as XLSX using Excel constants
|
# Save as XLSX using Excel constants
|
||||||
$xlOpenXMLWorkbook = 51 # Excel constant for XLSX format
|
$xlOpenXMLWorkbook = 51 # Excel constant for XLSX format
|
||||||
$xlNormal = 1 # Excel constant for normal save
|
|
||||||
|
|
||||||
# Use the standard SaveAs method
|
# Create a temporary filename without spaces
|
||||||
$workbook.SaveAs($excelXlsxPath, $xlOpenXMLWorkbook)
|
$tempXlsxPath = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName() + ".xlsx")
|
||||||
$workbook.Close($true) # True to save changes
|
|
||||||
|
|
||||||
Start-Sleep -Seconds 2 # Give Excel more time to finish saving
|
# Save to temporary file first
|
||||||
|
$workbook.SaveAs([string]$tempXlsxPath, $xlOpenXMLWorkbook)
|
||||||
|
$workbook.Close($true)
|
||||||
|
|
||||||
# Verify the file was created using robust path checking
|
# After Excel is closed, copy the file to the desired location
|
||||||
$verifyPath = [System.IO.Path]::GetFullPath($xlsxPath)
|
if (Test-Path -LiteralPath $tempXlsxPath) {
|
||||||
if (-not (Test-Path -LiteralPath $verifyPath)) {
|
Copy-Item -LiteralPath $tempXlsxPath -Destination $xlsxPath -Force
|
||||||
throw "Excel SaveAs succeeded but file not found at: $verifyPath"
|
Remove-Item -LiteralPath $tempXlsxPath -Force
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw "Excel SaveAs failed to create temporary file"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
@ -506,7 +511,9 @@ function Convert-Files {
|
|||||||
[System.GC]::WaitForPendingFinalizers()
|
[System.GC]::WaitForPendingFinalizers()
|
||||||
|
|
||||||
# Force Excel process cleanup
|
# Force Excel process cleanup
|
||||||
Get-Process -Name "EXCEL" -ErrorAction SilentlyContinue | Where-Object { $_.SI -eq [System.Security.Principal.WindowsIdentity]::GetCurrent().SessionId } | Stop-Process -Force
|
Get-Process -Name "EXCEL" -ErrorAction SilentlyContinue |
|
||||||
|
Where-Object { $_.SI -eq [System.Security.Principal.WindowsIdentity]::GetCurrent().SessionId } |
|
||||||
|
Stop-Process -Force
|
||||||
}
|
}
|
||||||
|
|
||||||
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user