Export Excel to ImageSource in WPF

Besides converting a workbook to a different file format (like PDF, HTML, and image), GemBox.Spreadsheet also supports converting worksheets to ImageSource objects with the ExcelFile.ConvertToImageSource method.

If you require more than displaying a file's content, for instance if you want to be able to modify the spreadsheet data with some Graphical User Interface (GUI) control control, then what you could do is export Excel to DataTable and bind it to DataGrid control or you could export Excel to DataGridView control.

The following example shows how you can convert a sheet from an Excel file to an image and attach it to WPF's Image control.

Converted sheet from Excel file to ImageSource and attached to WPF's Image control
Screenshot of Excel file exported to WPF's Image control
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Export to ImageSource Example" 
    SizeToContent="WidthAndHeight">

    <ScrollViewer>
        <Border Margin="10" BorderBrush="Black" BorderThickness="1">
            <Image x:Name="ImageControl"/>
        </Border>
    </ScrollViewer>

</Window>
using System.Windows;
using System.Windows.Controls;
using GemBox.Spreadsheet;

partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        SetImageSource(this.ImageControl);
    }

    private static void SetImageSource(Image image)
    {
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();

        var worksheet = workbook.Worksheets.Add("Sheet1");

        worksheet.Cells[0, 0].Value = "English:";
        worksheet.Cells[0, 1].Value = "Hello";

        worksheet.Cells[1, 0].Value = "Russian:";
        worksheet.Cells[1, 1].Value = new string(new char[] { '\u0417', '\u0434', '\u0440', '\u0430', '\u0432', '\u0441', '\u0442', '\u0432', '\u0443', '\u0439', '\u0442', '\u0435' });

        worksheet.Cells[2, 0].Value = "Chinese:";
        worksheet.Cells[2, 1].Value = new string(new char[] { '\u4f60', '\u597d' });

        worksheet.Cells[4, 0].Value = "In order to see Russian and Chinese characters you need to have appropriate fonts on your PC.";
        worksheet.Cells.GetSubrangeAbsolute(4, 0, 4, 7).Merged = true;

        worksheet.HeadersFooters.DefaultPage.Header.CenterSection.Content = "Export To ImageSource / Image Control Example";

        worksheet.PrintOptions.PrintGridlines = true;

        image.Source = workbook.ConvertToImageSource(SaveOptions.ImageDefault);
    }
}
Imports System.Windows
Imports System.Windows.Controls
Imports GemBox.Spreadsheet

Class MainWindow

    Public Sub New()

        InitializeComponent()

        SetImageSource(Me.ImageControl)
    End Sub

    Private Shared Sub SetImageSource(image As Image)

        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook = New ExcelFile

        Dim worksheet = workbook.Worksheets.Add("Sheet1")

        worksheet.Cells(0, 0).Value = "English:"
        worksheet.Cells(0, 1).Value = "Hello"

        worksheet.Cells(1, 0).Value = "Russian:"
        worksheet.Cells(1, 1).Value = New String(New Char() {ChrW(&H417), ChrW(&H434), ChrW(&H440), ChrW(&H430), ChrW(&H432), ChrW(&H441), ChrW(&H442), ChrW(&H432), ChrW(&H443), ChrW(&H439), ChrW(&H442), ChrW(&H435)})

        worksheet.Cells(2, 0).Value = "Chinese:"
        worksheet.Cells(2, 1).Value = New String(New Char() {ChrW(&H4F60), ChrW(&H597D)})

        worksheet.Cells(4, 0).Value = "In order to see Russian and Chinese characters you need to have appropriate fonts on your PC."
        worksheet.Cells.GetSubrangeAbsolute(4, 0, 4, 7).Merged = True

        worksheet.HeadersFooters.DefaultPage.Header.CenterSection.Content = "Export To ImageSource / Image Control Example"

        worksheet.PrintOptions.PrintGridlines = True

        image.Source = workbook.ConvertToImageSource(SaveOptions.ImageDefault)
    End Sub
End Class

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy