ExcelFileHandling

This commit is contained in:
Bobby Abellana 2025-02-24 15:04:58 -08:00
parent 3b74aba8ed
commit 38f5793883

View File

@ -473,7 +473,7 @@ function Convert-Files {
try { try {
# Create a safe temporary path without spaces # Create a safe temporary path without spaces
$safeTempDir = Join-Path $env:TEMP "XLSConversion" $safeTempDir = Join-Path $env:TEMP "XLSConversion"
if (-not (Test-Path $safeTempDir)) { if (-not (Test-Path -Path $safeTempDir)) {
New-Item -ItemType Directory -Path $safeTempDir -Force | Out-Null New-Item -ItemType Directory -Path $safeTempDir -Force | Out-Null
} }
@ -494,8 +494,16 @@ function Convert-Files {
} }
$script:txtStatus.Text += "Opening workbook from: $safeOriginalPath`n" $script:txtStatus.Text += "Opening workbook from: $safeOriginalPath`n"
try {
# Try opening with different recovery options
try { try {
$workbook = $excel.Workbooks.Open($safeOriginalPath) $workbook = $excel.Workbooks.Open($safeOriginalPath)
}
catch {
$script:txtStatus.Text += "Standard open failed, trying with OpenAndRepair...`n"
$workbook = $excel.Workbooks.OpenAndRepair($safeOriginalPath)
}
$script:txtStatus.Text += "Workbook opened successfully`n" $script:txtStatus.Text += "Workbook opened successfully`n"
# Create safe XLSX path # Create safe XLSX path
@ -505,17 +513,74 @@ function Convert-Files {
$script:txtStatus.Text += "Attempting to save as XLSX: $safeXlsxPath`n" $script:txtStatus.Text += "Attempting to save as XLSX: $safeXlsxPath`n"
# Try multiple SaveAs approaches with progressive fallbacks
$saveSuccess = $false
# Approach 1: Standard SaveAs with explicit format
try { try {
# Try different SaveAs approaches $script:txtStatus.Text += "Trying standard SaveAs method...`n"
try { $excel.DisplayAlerts = $false
# Approach 1: Use FileFormat property $workbook.SaveAs([string]$safeXlsxPath, [int]51) # 51 = xlOpenXMLWorkbook
$excel.DefaultSaveFormat = 51 # xlOpenXMLWorkbook $saveSuccess = $true
$workbook.SaveAs($safeXlsxPath)
} }
catch { catch {
$script:txtStatus.Text += "First save attempt failed, trying alternate method...`n" $script:txtStatus.Text += "Standard SaveAs failed: $($_.Exception.Message)`n"
# Approach 2: Use explicit FileFormat }
# Approach 2: Try with compatibility options disabled
if (-not $saveSuccess) {
try {
$script:txtStatus.Text += "Trying SaveAs with compatibility options disabled...`n"
$excel.DisplayAlerts = $false
if ($workbook.PSObject.Properties.Name -contains "CheckCompatibility") {
$workbook.CheckCompatibility = $false
}
$workbook.SaveAs([string]$safeXlsxPath, [int]51) $workbook.SaveAs([string]$safeXlsxPath, [int]51)
$saveSuccess = $true
}
catch {
$script:txtStatus.Text += "Compatibility SaveAs failed: $($_.Exception.Message)`n"
}
}
# Approach 3: Try with Excel 2007-2013 format
if (-not $saveSuccess) {
try {
$script:txtStatus.Text += "Trying SaveAs with Excel 2007-2013 format...`n"
$excel.DisplayAlerts = $false
$workbook.SaveAs([string]$safeXlsxPath, [int]52) # 52 = xlExcel12 (Excel 2007-2013)
$saveSuccess = $true
}
catch {
$script:txtStatus.Text += "Excel 2007-2013 SaveAs failed: $($_.Exception.Message)`n"
}
}
# Approach 4: Last resort - try with Excel binary format then convert again
if (-not $saveSuccess) {
try {
$script:txtStatus.Text += "Trying intermediate binary format conversion...`n"
$excel.DisplayAlerts = $false
# First save as Excel Binary Workbook
$safeBinaryPath = [System.IO.Path]::ChangeExtension($safeXlsxPath, ".xlsb")
$workbook.SaveAs([string]$safeBinaryPath, [int]50) # 50 = xlExcel12 (Excel Binary)
$workbook.Close($false)
# Then open the binary and save as XLSX
$binaryWorkbook = $excel.Workbooks.Open($safeBinaryPath)
$binaryWorkbook.SaveAs([string]$safeXlsxPath, [int]51)
$binaryWorkbook.Close($false)
$saveSuccess = $true
}
catch {
$script:txtStatus.Text += "Binary conversion failed: $($_.Exception.Message)`n"
}
}
if (-not $saveSuccess) {
throw "All conversion methods failed. Unable to convert file."
} }
Start-Sleep -Seconds 2 # Give the file system time to catch up Start-Sleep -Seconds 2 # Give the file system time to catch up
@ -535,19 +600,12 @@ function Convert-Files {
$script:txtStatus.Text += "Successfully saved XLSX file`n" $script:txtStatus.Text += "Successfully saved XLSX file`n"
} }
catch { catch {
$script:txtStatus.Text += "Error during SaveAs: $($_.Exception.Message)`n" $script:txtStatus.Text += "Error during Excel operations: $($_.Exception.Message)`n"
if ($_.Exception.HResult) {
$script:txtStatus.Text += "Error HResult: $($_.Exception.HResult)`n"
}
throw
}
}
catch {
$script:txtStatus.Text += "Error opening workbook: $($_.Exception.Message)`n"
throw throw
} }
} }
finally { finally {
# Thorough cleanup of Excel objects
if ($workbook) { if ($workbook) {
try { try {
$workbook.Close($false) $workbook.Close($false)
@ -556,6 +614,14 @@ function Convert-Files {
catch { } catch { }
$workbook = $null $workbook = $null
} }
if ($binaryWorkbook) {
try {
$binaryWorkbook.Close($false)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($binaryWorkbook) | Out-Null
}
catch { }
$binaryWorkbook = $null
}
if ($excel) { if ($excel) {
try { try {
$excel.Quit() $excel.Quit()