Functions/Convert-ExcelToHtml.ps1
<#
.SYNOPSIS This function converts an Excel workbook, worksheet or range of cells to HTML. #> function Convert-ExcelToHtml { [CmdletBinding(PositionalBinding=$true)] [OutputType([Bool])] param ( # The path for the Excel workbook file. [Parameter(Mandatory=$true, Position=0)] [ValidateNotNullOrEmpty()] [ValidateScript({(Test-Path -Path $_ -ErrorAction SilentlyContinue)})] [String]$excelWorkbookFilePath, # The path for the HTML file. [Parameter(Mandatory=$true, Position=1)] [ValidateNotNullOrEmpty()] [ValidateScript({(Test-Path -Path (Split-Path $_ -Parent) -ErrorAction SilentlyContinue)})] [String]$htmlFilePath, # The path for the folder containing the XLS Converter program installation. [Parameter(Mandatory=$true, Position=2)] [ValidateNotNullOrEmpty()] [ValidateScript({(Test-Path -Path $_ -ErrorAction SilentlyContinue)})] [String]$xlsConverterFolderPath, # The name of worksheet to convert. [Parameter(Mandatory=$true, Position=3)] [ValidateNotNullOrEmpty()] [String]$worksheetName ) try { # Import the XLS Converter Functions from the Spire DLL ImportXLSConverterFunctions -XlsConverterFolderPath $xlsConverterFolderPath # Create Spire.XLS workbook and load it with the template contents Write-Information "Creating a Spire.XLS workbook with the Excel file contents." $workbook = [Spire.Xls.Workbook]::new() $workbook.LoadFromFile($excelWorkbookFilePath, [Spire.Xls.ExcelVersion]::Version2016) # Perform the conversion $workbook.CalculateAllValue(); $workbook.ConverterSetting.SheetFitToPage = $true $workbook.Worksheets[$worksheetName].SaveToHtml($htmlFilePath) # Success return $true } catch { Write-Error "Error on line $($_.InvocationInfo.ScriptLineNumber) while converting Excel workbook to HTML. `r`n$($_.Exception.Message)" return $false } } |