I am using FreeSpire.XLS to (attempt to) export all charts, in all sheets, of an XLSX file to .PNGs. In this example, my XLSX has 6 sheets, each containing a single graph. Below is my code (written in PowerShell). When running this code, it only exports up to sheet 3 (2). It can see up to sheet 6 (5), but doesn't export an image for anything above sheet 3 (2). More specifically, it doesn't seem to see that there is a chart on anything above sheet 3 (2), despite the fact that there is. Is this a bug?
- Code: Select all
# Load the Spire.XLS assembly
Add-Type -Path "C:\schubgus\Spire.XLS.dll"
# Create an instance of the Workbook class
$workbook = New-Object Spire.Xls.Workbook
# Load a sample Excel file
$workbook.LoadFromFile("C:\schubgus\BD.xlsx")
# Iterate through all worksheets
foreach ($sheet in $workbook.Worksheets)
{
Write-Host "Processing worksheet $($sheet.Name) with index $($sheet.Index)"
# Check if the worksheet has charts
if ($sheet.Charts.Count -gt 0)
{
# Save charts in the current worksheet as images
$imgs = $workbook.SaveChartAsImage($sheet)
# Output the count of images returned
Write-Host "Number of images returned: $($imgs.Length)"
# Save the images to png files
for ($i = 0; $i -lt $imgs.Length; $i++)
{
# Check if the current image is not null before saving
if ($imgs[$i] -ne $null)
{
$fileName = "C:\schubgus\chart-$($sheet.Name)-$($sheet.Index).png"
$imgs[$i].Save($fileName, [System.Drawing.Imaging.ImageFormat]::Png)
Write-Host "Image $i saved for worksheet $($sheet.Name) with index $($sheet.Index) to $fileName"
}
else
{
Write-Host "Image $i is null and cannot be saved for worksheet $($sheet.Name) with index $($sheet.Index)."
}
}
}
else
{
Write-Host "No charts found in worksheet $($sheet.Name) with index $($sheet.Index)."
}
}
Note that I have already validated that there is nothing wrong with my XLSX file, or my installation of FreeSpire.XLS. I have full administrative rights on this computer.