ERROR MESSAGE : Aspose.Cells.CellsException: Image dimensions (width=56972 height=179427) are too large. If you have enabled OnePagePerSheet, please disable it.
how to solved this
// === Remove margins ===
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
// Define image save options
var imageOptions = new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = ImageType.Png,
HorizontalResolution = 900,
VerticalResolution = 900
};
// Render worksheet to image
var sheetRender = new SheetRender(worksheet, imageOptions);
// Save the first page (index 0) as PNG image
sheetRender.ToImage(0, newFilePath);
// Read the image file bytes
byte[] fileBytes = System.IO.File.ReadAllBytes(newFilePath);
// Convert to Base64 data URL
string base64DataUrl = $"data:image/png;base64,{Convert.ToBase64String(fileBytes)}";
// Dispose workbook explicitly (recommended)
workbook.Dispose();
bool fileExists = System.IO.File.Exists(newFilePath);
if (fileExists)
{
System.IO.File.Delete(newFilePath);
}
return JsonConvert.SerializeObject((new { fileName = fullFileName, fileImage = base64DataUrl }));
@imparthgalani
The error you’re encountering indicates that the image dimensions generated when using the OnePagePerSheet
option are exceeding the maximum allowed size. To resolve this issue, follow these steps:
-
Disable OnePagePerSheet: If you don’t specifically need each sheet to be rendered as a single image, consider disabling this option. This will allow the images to be generated based on the actual page size settings of the worksheet.
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.OnePagePerSheet = false; // Disable this option
-
Adjust Image Resolution: If you need to keep OnePagePerSheet
enabled, try reducing the resolution of the output images. This can help keep the dimensions within acceptable limits.
imgOptions.HorizontalResolution = 96; // Set a lower resolution
imgOptions.VerticalResolution = 96; // Set a lower resolution
-
Set Desired Size: You can also specify a desired size for the output images, which can help control the dimensions.
imgOptions.SetDesiredSize(1920, 1080); // Set a maximum width and height
-
Check Content Size: Ensure that the content in your Excel sheets is not excessively large or complex, as this can also lead to large image dimensions.
By following these steps, you should be able to resolve the issue and successfully convert your Excel file to PNG images. If you encounter any further issues or need additional assistance, please let me know!
@imparthgalani,
As the error indicates, the image dimensions are too large to enable the OnePagePerSheet option during rendering. To resolve this issue, either disable the option or minimize the image dimensions. Please use the line of code instead:
OnePagePerSheet = false,
OnePagePerSheet = true
using how to possible because i want to create an image single page
@imparthgalani,
This won’t be possible every time. It entirely depends on the data, content, or objects within the worksheet. If the worksheet contains limited data or content, then OnePagePerSheet can work effectively. However, if the worksheet includes extensive data or multiple pages, it may not be feasible to render everything on a single page. In such cases, adjustments like scaling, page breaks, or customizing the layout might be necessary to achieve the desired output.