Charts in Excel are powerful tools for visualizing data. However, when sharing reports or integrating charts into other applications like websites or presentations, you might need them as images rather than native Excel objects. This tutorial explains how to convert an Excel chart to an image using Aspose.Cells for .NET.

Introduction

Charts in Excel are a great way to visualize data. However, when sharing reports or integrating charts into other applications like websites or presentations, you might need them as images rather than native Excel objects. This tutorial explains how to convert an Excel chart to an image using Aspose.Cells for .NET.

Step-by-Step Guide

Step 1: Install Aspose.Cells for .NET

To start working with Aspose.Cells, you need to install the package via NuGet Package Manager:

$ dotnet add package Aspose.Cells

Step 2: Load the Workbook

Load your Excel workbook and access the worksheet containing the chart.

Workbook workbook = new Workbook("Dashboard.xlsx");
Worksheet sheet = workbook.Worksheets["Charts"];

Step 3: Select the Chart

Access the desired chart from the Charts collection of the worksheet.

Chart chart = sheet.Charts[0];

Step 4: Set Export Options

Configure the export options to specify image type, resolution, and other settings.

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

Step 5: Convert Chart to Image

Use the ToImage method of the chart object to export it as an image.

chart.ToImage("chart_output.png", options);

Step 6: Save and Verify

This saves your chart as a PNG file. You can also choose JPEG, BMP, TIFF, etc.

Step 7: Optional Enhancements

You can control image size and quality further by setting:

chart.ToImage("chart_highres.png", new ImageOrPrintOptions
{
    ImageType = ImageType.Jpeg,
    SmoothingMode = SmoothingMode.AntiAlias,
    ChartImageWidth = 1200,
    ChartImageHeight = 800
});

Complete Example Code

Here is a complete example that combines all the steps into one program:

using System;
using Aspose.Cells;
using Aspose.Cells.Rendering;
using System.Drawing.Drawing2D;

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

        // Access the worksheet and first chart
        Worksheet sheet = workbook.Worksheets["Charts"];
        Chart chart = sheet.Charts[0];

        // Set image export options
        ImageOrPrintOptions options = new ImageOrPrintOptions
        {
            ImageType = ImageType.Png,
            HorizontalResolution = 300,
            VerticalResolution = 300,
            SmoothingMode = SmoothingMode.AntiAlias,
            ChartImageWidth = 1200,
            ChartImageHeight = 800
        };

        // Export chart to image
        chart.ToImage("chart_output.png", options);

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

Common Scenarios & Fixes

IssueSolution
Chart appears blurryIncrease ChartImageWidth and ChartImageHeight
Image lacks clarityUse SmoothingMode = AntiAlias
Chart is clippedCheck worksheet margins or scaling settings

More in this category