How to Customize Excel to Image Rendering in C#

How to Customize Excel to Image Rendering in C#

When exporting Excel worksheets or ranges to images, you often need precise control over how the content looks. Whether for printing, presentation, or embedding, this article demonstrates how to customize image rendering using Aspose.Cells for .NET.

Why Customize Rendering?

  • Improve image clarity and resolution
  • Show or hide gridlines, formulas, or headings
  • Match layout and sizing to documentation requirements
  • Adjust background transparency or borders

Step-by-Step Guide

Step 1: Install Aspose.Cells for .NET

dotnet add package Aspose.Cells

Step 2: Load the Workbook and Worksheet

Workbook workbook = new Workbook("Template.xlsx");
Worksheet worksheet = workbook.Worksheets["Sheet1"];

Step 3: Configure Advanced Rendering Options

ImageOrPrintOptions options = new ImageOrPrintOptions
{
    ImageType = ImageType.Png,
    HorizontalResolution = 300,
    VerticalResolution = 300,
    PrintWithStatusDialog = false,
    Transparent = false,
    OnePagePerSheet = true
};

Step 4: Enable Gridlines and Headings (Optional)

// Show gridlines in the output
options.ShowGridLines = true;

// Render row/column headings
options.ShowRowColumnHeaders = true;

Step 5: Customize Page Settings

// Set to render entire sheet as a single page
options.AllColumnsInOnePagePerSheet = true;
options.AllRowsInOnePagePerSheet = true;

Step 6: Render to Image with SheetRender

SheetRender renderer = new SheetRender(worksheet, options);
renderer.ToImage(0, "custom_output.png");

Step 7: Save and Verify Output

Ensure that the resulting image reflects your custom layout and visual choices.


Complete Example Code

using System;
using Aspose.Cells;

class Program
{
    static void Main()
    {
        // Load Excel file
        Workbook workbook = new Workbook("Template.xlsx");
        Worksheet worksheet = workbook.Worksheets["Sheet1"];

        // Set up advanced rendering options
        ImageOrPrintOptions options = new ImageOrPrintOptions
        {
            ImageType = ImageType.Png,
            HorizontalResolution = 300,
            VerticalResolution = 300,
            PrintWithStatusDialog = false,
            Transparent = false,
            OnePagePerSheet = true,
            ShowGridLines = true,
            ShowRowColumnHeaders = true,
            AllColumnsInOnePagePerSheet = true,
            AllRowsInOnePagePerSheet = true
        };

        // Render to image
        SheetRender renderer = new SheetRender(worksheet, options);
        renderer.ToImage(0, "custom_output.png");

        Console.WriteLine("Custom-rendered worksheet saved as image.");
    }
}

Tips for Better Control

SettingDescription
ImageTypeChoose PNG, JPEG, BMP, or TIFF
TransparentSet true for transparent background
SmoothingModeImprove visuals using anti-aliasing
ShowFormulasShow formulas instead of calculated values
ChartImageWidth / ChartImageHeightSet output size for charts
 English