Exporting a single Excel worksheet to an image format such as PNG or JPEG is useful for generating previews, exporting formatted reports, and embedding sheets in web pages or PDFs. This guide demonstrates how to convert one worksheet from an Excel workbook into images using Aspose.Cells for .NET.

Introduction

Exporting a single Excel worksheet to an image format (e.g., PNG, JPEG) is useful when generating previews, exporting charts, or sharing read-only visual representations of spreadsheet content. This guide shows you how to convert one worksheet from an Excel workbook to an image using Aspose.Cells for .NET.

Use Cases

  • Generate a preview of a specific worksheet
  • Export formatted reports for email or documentation
  • Embed a single sheet in a web page or PDF

Step-by-Step Guide

Step 1: Install Aspose.Cells for .NET

$ dotnet add package Aspose.Cells

Step 2: Load the Excel File

Workbook workbook = new Workbook("SalesData.xlsx");
Worksheet sheet = workbook.Worksheets["Q1 Report"]; // Access specific worksheet

Step 3: Define Image Rendering Options

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

Step 4: Create SheetRender Object

SheetRender renderer = new SheetRender(sheet, options);

Step 5: Render Each Page to an Image

for (int pageIndex = 0; pageIndex < renderer.PageCount; pageIndex++)
{
    string imageName = $"worksheet_q1_page_{pageIndex + 1}.png";
    renderer.ToImage(pageIndex, imageName);
}

Step 6: Save the Images

This code automatically saves one image per printable page in the worksheet.

Step 7: Optional Enhancements

You can apply additional layout settings:

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

// Fit all content on a single page
options.AllColumnsInOnePagePerSheet = true;

Complete Example Code

using System;
using Aspose.Cells;
class Program
{
    static void Main()
    {
        // Load the Excel workbook
        Workbook workbook = new Workbook("SalesData.xlsx");

        // Access a specific worksheet
        Worksheet sheet = workbook.Worksheets["Q1 Report"];

        // Define image rendering options
        ImageOrPrintOptions options = new ImageOrPrintOptions
        {
            ImageType = ImageType.Png,
            OnePagePerSheet = true,
            HorizontalResolution = 200,
            VerticalResolution = 200,
            PrintingPageType = PrintingPageType.Default
        };

        // Enable gridlines if desired
        options.ShowGridLines = true;

        // Render the sheet to image(s)
        SheetRender renderer = new SheetRender(sheet, options);

        for (int pageIndex = 0; pageIndex < renderer.PageCount; pageIndex++)
        {
            string imageName = $"worksheet_q1_page_{pageIndex + 1}.png";
            renderer.ToImage(pageIndex, imageName);
            Console.WriteLine($"Saved: {imageName}");
        }

        Console.WriteLine("Worksheet successfully rendered to image(s).");
    }
}

Common Scenarios & Troubleshooting

IssueSolution
Cut-off contentUse AllColumnsInOnePagePerSheet = true
Output is low qualityIncrease image resolution
Gridlines missingSet ShowGridLines = true

More in this category