Compare commits

..

2 Commits

Author SHA1 Message Date
Bobby Abellana
38f5793883 ExcelFileHandling 2025-02-24 15:04:58 -08:00
Bobby Abellana
3b74aba8ed Updated Readme 2025-02-24 14:57:02 -08:00
3 changed files with 125 additions and 39 deletions

View File

@ -53,14 +53,14 @@ catch {
1. Microsoft Excel 2013 or newer 1. Microsoft Excel 2013 or newer
2. PowerShell must run in STA mode 2. PowerShell must run in STA mode
Run the main script like this: Run the main script like this:
powershell.exe -STA -File .\MainForm.ps1 pwsh -STA -File .\MainForm.ps1
"@ -ForegroundColor Red "@ -ForegroundColor Red
exit 1 exit 1
} }
Write-Host @" Write-Host @"
Installation complete! Run the main application with: Installation complete! Run the main application with:
powershell.exe -STA -File .\MainForm.ps1 pwsh -STA -File .\MainForm.ps1
Note: First run might take longer while modules load Note: First run might take longer while modules load
"@ -ForegroundColor Green "@ -ForegroundColor Green

View File

@ -6,9 +6,29 @@ This tool allows you to convert legacy `.xls` files stored in SharePoint to the
### Step 1: Install PowerShell 7.5 ### Step 1: Install PowerShell 7.5
**Option 1: Manual Installation**
1. Visit the [PowerShell GitHub releases page](https://github.com/PowerShell/PowerShell/releases) 1. Visit the [PowerShell GitHub releases page](https://github.com/PowerShell/PowerShell/releases)
2. Download the appropriate installer for your system (e.g., `PowerShell-7.5.0-win-x64.msi` for 64-bit Windows) 2. Download the appropriate installer for your system (e.g., `PowerShell-7.5.0-win-x64.msi` for 64-bit Windows)
3. Run the installer and follow the on-screen instructions 3. Run the installer and follow the on-screen instructions
**Option 2: Install using winget (Windows 10/11)**
1. Open a command prompt or PowerShell window
2. Run the following command:
```
winget install --id Microsoft.PowerShell
```
**Option 3: Install using PowerShell script**
1. Open Windows PowerShell as administrator
2. Run the following commands:
```powershell
# Download the installation script
Invoke-WebRequest -Uri https://aka.ms/install-powershell.ps1 -OutFile install-powershell.ps1
# Install PowerShell 7.5
.\install-powershell.ps1 -UseMSI -Quiet
```
4. Verify the installation by opening PowerShell 7 from the Start menu 4. Verify the installation by opening PowerShell 7 from the Start menu
### Step 2: Install Required Dependencies ### Step 2: Install Required Dependencies
@ -17,7 +37,7 @@ This tool allows you to convert legacy `.xls` files stored in SharePoint to the
2. Right-click on PowerShell 7 in the Start menu and select "Run as administrator" 2. Right-click on PowerShell 7 in the Start menu and select "Run as administrator"
3. Navigate to the folder containing the converter files: 3. Navigate to the folder containing the converter files:
``` ```
cd C:\path\to\XLS_to_XLSX cd C:\path\to\SharepointXLS_to_XLSX
``` ```
4. Run the dependency installer script: 4. Run the dependency installer script:
``` ```
@ -37,7 +57,7 @@ This tool allows you to convert legacy `.xls` files stored in SharePoint to the
1. Open PowerShell 7 as administrator 1. Open PowerShell 7 as administrator
2. Navigate to the application folder: 2. Navigate to the application folder:
``` ```
cd C:\path\to\XLS_to_XLSX cd C:\path\to\SharepointXLS_to_XLSX
``` ```
3. Run the application in STA mode: 3. Run the application in STA mode:
``` ```

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()