How to Convert a Cell Range to Image in C#

How to Convert a Cell Range to Image in C#

Need to visually extract a portion of a spreadsheet? Exporting a cell range to an image is useful for generating thumbnails, previews, or partial reports. This guide shows how to convert a defined cell range in Excel into a high-quality image using Aspose.Cells for .NET.

Use Cases

  • Export pricing tables or product catalogs
  • Share part of a worksheet without exposing the full file
  • Capture dynamic ranges for dashboards or widgets

Step-by-Step Guide

Step 1: Install Aspose.Cells

dotnet add package Aspose.Cells

Step 2: Load the Workbook and Worksheet

Workbook workbook = new Workbook("DataSet.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

Step 3: Define the Range to Export

// Specify a range like A1:D10
Range range = worksheet.Cells.CreateRange("A1", "D10");

Step 4: Configure Image Rendering Options

ImageOrPrintOptions options = new ImageOrPrintOptions
{
    ImageType = ImageType.Png,
    HorizontalResolution = 200,
    VerticalResolution = 200,
    OnePagePerSheet = true
};

Step 5: Create a SheetRender and Render the Range

// You can use SheetRender with printing bounds if needed
SheetRender renderer = new SheetRender(worksheet, options);

Step 6: Export Range as Image

Aspose.Cells does not have a direct RangeRender class, but you can still clip an image by focusing rendering to just a selected range:

// Set print area manually for the worksheet
worksheet.PageSetup.PrintArea = "A1:D10";

// Recreate SheetRender with print settings now applied
renderer = new SheetRender(worksheet, options);

// Render and export
renderer.ToImage(0, "range_output.png");

Step 7: Save and Verify Output

You’ll now have a clean cropped image of the selected Excel range.


Complete Example Code

using System;
using Aspose.Cells;

class Program
{
    static void Main()
    {
        // Load the workbook
        Workbook workbook = new Workbook("DataSet.xlsx");

        // Access the first worksheet
        Worksheet worksheet = workbook.Worksheets[0];

        // Define range to export (A1 to D10)
        Range range = worksheet.Cells.CreateRange("A1", "D10");

        // Set the print area to this range
        worksheet.PageSetup.PrintArea = range.RefersTo;

        // Set image export options
        ImageOrPrintOptions options = new ImageOrPrintOptions
        {
            ImageType = ImageType.Png,
            HorizontalResolution = 200,
            VerticalResolution = 200,
            OnePagePerSheet = true
        };

        // Render only the specified range
        SheetRender renderer = new SheetRender(worksheet, options);

        // Export to image
        renderer.ToImage(0, "range_output.png");

        Console.WriteLine("Cell range exported successfully as image.");
    }
}

Troubleshooting Tips

IssueSolution
Image includes extra rows/columnsEnsure the print area is strictly defined
Range not cropped properlySet OnePagePerSheet = true
Blurry textIncrease resolution settings
 English