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 {
# Create a safe temporary path without spaces
$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
}
@ -495,7 +495,15 @@ function Convert-Files {
$script:txtStatus.Text += "Opening workbook from: $safeOriginalPath`n"
try {
$workbook = $excel.Workbooks.Open($safeOriginalPath)
# Try opening with different recovery options
try {
$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"
# Create safe XLSX path
@ -505,49 +513,99 @@ function Convert-Files {
$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 different SaveAs approaches
try {
# Approach 1: Use FileFormat property
$excel.DefaultSaveFormat = 51 # xlOpenXMLWorkbook
$workbook.SaveAs($safeXlsxPath)
}
catch {
$script:txtStatus.Text += "First save attempt failed, trying alternate method...`n"
# Approach 2: Use explicit FileFormat
$workbook.SaveAs([string]$safeXlsxPath, [int]51)
}
Start-Sleep -Seconds 2 # Give the file system time to catch up
if (-not (Test-Path -LiteralPath $safeXlsxPath)) {
throw "File was not created after SaveAs operation"
}
$script:txtStatus.Text += "Save successful, verifying file...`n"
$fileInfo = Get-Item -LiteralPath $safeXlsxPath
$script:txtStatus.Text += "Saved file size: $($fileInfo.Length) bytes`n"
# Copy the file back to the original destination with spaces
$script:txtStatus.Text += "Copying to final destination: $xlsxPath`n"
Copy-Item -LiteralPath $safeXlsxPath -Destination $xlsxPath -Force
$script:txtStatus.Text += "Successfully saved XLSX file`n"
$script:txtStatus.Text += "Trying standard SaveAs method...`n"
$excel.DisplayAlerts = $false
$workbook.SaveAs([string]$safeXlsxPath, [int]51) # 51 = xlOpenXMLWorkbook
$saveSuccess = $true
}
catch {
$script:txtStatus.Text += "Error during SaveAs: $($_.Exception.Message)`n"
if ($_.Exception.HResult) {
$script:txtStatus.Text += "Error HResult: $($_.Exception.HResult)`n"
}
throw
$script:txtStatus.Text += "Standard SaveAs failed: $($_.Exception.Message)`n"
}
# 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)
$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
if (-not (Test-Path -LiteralPath $safeXlsxPath)) {
throw "File was not created after SaveAs operation"
}
$script:txtStatus.Text += "Save successful, verifying file...`n"
$fileInfo = Get-Item -LiteralPath $safeXlsxPath
$script:txtStatus.Text += "Saved file size: $($fileInfo.Length) bytes`n"
# Copy the file back to the original destination with spaces
$script:txtStatus.Text += "Copying to final destination: $xlsxPath`n"
Copy-Item -LiteralPath $safeXlsxPath -Destination $xlsxPath -Force
$script:txtStatus.Text += "Successfully saved XLSX file`n"
}
catch {
$script:txtStatus.Text += "Error opening workbook: $($_.Exception.Message)`n"
$script:txtStatus.Text += "Error during Excel operations: $($_.Exception.Message)`n"
throw
}
}
finally {
# Thorough cleanup of Excel objects
if ($workbook) {
try {
$workbook.Close($false)
@ -556,6 +614,14 @@ function Convert-Files {
catch { }
$workbook = $null
}
if ($binaryWorkbook) {
try {
$binaryWorkbook.Close($false)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($binaryWorkbook) | Out-Null
}
catch { }
$binaryWorkbook = $null
}
if ($excel) {
try {
$excel.Quit()