Functions/Convert-ExcelToImage.ps1

<#
.SYNOPSIS
    This function converts an Excel worksheet to an image.
#>

function Convert-ExcelToImage {
    [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 image file.
        [Parameter(Mandatory=$true, Position=1)]
        [ValidateNotNullOrEmpty()]
        [ValidateScript({(Test-Path -Path (Split-Path $_ -Parent) -ErrorAction SilentlyContinue)})]
        [String]$imageFilePath,

        # 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.ConverterSetting.SheetFitToPage = $true
        $workbook.Worksheets[$worksheetName].SaveToImage($imageFilePath)

        # Success
        return $true
    }
    catch {
        Write-Error "Error on line $($_.InvocationInfo.ScriptLineNumber) while converting Excel workbook to an image. `r`n$($_.Exception.Message)"
        return $false
    }
}