When working with document preview features or content libraries, thumbnail images provide a quick visual reference without loading the entire file. This tutorial demonstrates how to create thumbnail images from Excel files using Aspose.Cells for .NET.

Introduction

Creating thumbnails of Excel files can be incredibly useful in various applications such as document galleries, dashboards, and search tools. Thumbnails offer a quick visual reference without the need to load entire documents, making them ideal for lightweight previews.

In this tutorial, we will explore how to generate thumbnail images from Excel files using Aspose.Cells for .NET. We’ll cover installation, rendering options, resizing techniques, and best practices to ensure high-quality thumbnails that are optimized for performance.

Step-by-Step Guide

Step 1: Install Aspose.Cells

Before you start creating thumbnails, make sure you have Aspose.Cells for .NET installed. You can install it via the NuGet Package Manager with the following command:

Install-Package Aspose.Cells

Step 2: Load the Workbook

Once Aspose.Cells is installed, load your Excel workbook and access the worksheet you want to convert into a thumbnail.

Workbook workbook = new Workbook("FinanceReport.xlsx");
Worksheet sheet = workbook.Worksheets[0];

Step 3: Configure Thumbnail Rendering Options

To generate thumbnails, configure the rendering options for low-resolution images. This ensures that the generated thumbnail is lightweight and quick to load.

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

Step 4: Render the First Worksheet to an Image

Use SheetRender to render the worksheet as a temporary image file.

SheetRender renderer = new SheetRender(sheet, options);
renderer.ToImage(0, "preview_temp.png");

Step 5: Resize Image to Thumbnail Dimensions (Optional)

You can use System.Drawing to resize the temporary image file to your desired thumbnail dimensions.

using System.Drawing;
Bitmap original = new Bitmap("preview_temp.png");
Bitmap thumbnail = new Bitmap(original, new Size(160, 120));
thumbnail.Save("thumbnail.png");

Step 6: Use the Thumbnail Image

Now you have a lightweight thumbnail suitable for previews.

Complete Example Code

Here is the complete example code that combines all the steps mentioned above:

using System;
using System.Drawing;
using Aspose.Cells;
using Aspose.Cells.Rendering;
class Program
{
    static void Main()
    {
        // Load Excel file
        Workbook workbook = new Workbook("FinanceReport.xlsx");
        Worksheet sheet = workbook.Worksheets[0];

        // Configure low-resolution options
        ImageOrPrintOptions options = new ImageOrPrintOptions
        {
            ImageType = ImageType.Png,
            HorizontalResolution = 96,
            VerticalResolution = 96,
            OnePagePerSheet = true
        };

        // Render full sheet as a temporary image
        SheetRender renderer = new SheetRender(sheet, options);
        renderer.ToImage(0, "preview_temp.png");

        // Resize to thumbnail
        using (Bitmap original = new Bitmap("preview_temp.png"))
        {
            Bitmap thumbnail = new Bitmap(original, new Size(160, 120));
            thumbnail.Save("thumbnail.png");
        }

        Console.WriteLine("Thumbnail image created from Excel worksheet.");
    }
}

Best Practices

RecommendationPurpose
Use OnePagePerSheet = truePrevent multi-page splitting in image
Lower resolutionOptimizes for faster thumbnail generation
Resize image after renderingMore control over scaling and quality

More in this category