Export Excel to ImageSource in WPF

Following sample shows how to convert workbook page to ImageSource instance and attach it to WPF's Image control.

Screenshot

Export to ImageSource / Image Control Screenshot

See the full code below.

<Window x:Class="ConvertToImageSourceCs.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Export to ImageSource Sample" 
        SizeToContent="WidthAndHeight">
        
    <Border Margin="10" BorderBrush="Black" BorderThickness="1">
        <Image x:Name="ImageControl"/>
    </Border>       
    
</Window>
1using System.Windows;
2using System.Windows.Controls;
3using GemBox.Spreadsheet;
4
5namespace ConvertToImageSourceCs
6{
7    public partial class MainWindow : Window
8    {
9        public MainWindow()
10        {
11            InitializeComponent();
12
13            SetImageSource(this.ImageControl);
14        }
15
16        private static void SetImageSource(Image image)
17        {
18            SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
19
20            ExcelFile ef = new ExcelFile();
21
22            var ws = ef.Worksheets.Add("Sheet1");
23
24            ws.Cells[0, 0].Value = "English:";
25            ws.Cells[0, 1].Value = "Hello";
26
27            ws.Cells[1, 0].Value = "Russian:";
28            ws.Cells[1, 1].Value = new string(new char[] { '\u0417', '\u0434', '\u0440', '\u0430', '\u0432', '\u0441', '\u0442', '\u0432', '\u0443', '\u0439', '\u0442', '\u0435' });
29
30            ws.Cells[2, 0].Value = "Chinese:";
31            ws.Cells[2, 1].Value = new string(new char[] { '\u4f60', '\u597d' });
32
33            ws.Cells[4, 0].Value = "In order to see Russian and Chinese characters you need to have appropriate fonts on your PC.";
34            ws.Cells.GetSubrangeAbsolute(4, 0, 4, 7).Merged = true;
35
36            ws.HeadersFooters.DefaultPage.Header.CenterSection.Content = "Export To ImageSource / Image Control Sample";
37
38            ws.PrintOptions.PrintGridlines = true;
39
40            image.Source = ef.ConvertToImageSource(SaveOptions.ImageDefault);
41        }
42    }
43}
1Imports GemBox.Spreadsheet
2
3Class MainWindow
4
5    Public Sub New()
6        InitializeComponent()
7
8        SetImageSource(Me.ImageControl)
9    End Sub
10
11    Private Shared Sub SetImageSource(image As Image)
12        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
13
14        Dim ef As New ExcelFile()
15
16        Dim ws = ef.Worksheets.Add("Sheet1")
17
18        ws.Cells(0, 0).Value = "English:"
19        ws.Cells(0, 1).Value = "Hello"
20
21        ws.Cells(1, 0).Value = "Russian:"
22        ws.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)})
23
24        ws.Cells(2, 0).Value = "Chinese:"
25        ws.Cells(2, 1).Value = New String(New Char() {ChrW(&H4F60), ChrW(&H597D)})
26
27        ws.Cells(4, 0).Value = "In order to see Russian and Chinese characters you need to have appropriate fonts on your PC."
28        ws.Cells.GetSubrangeAbsolute(4, 0, 4, 7).Merged = True
29
30        ws.HeadersFooters.DefaultPage.Header.CenterSection.Content = "Export To ImageSource / Image Control Sample"
31
32        ws.PrintOptions.PrintGridlines = True
33
34        image.Source = ef.ConvertToImageSource(SaveOptions.ImageDefault)
35    End Sub
36
37End Class

Check next sample or find out more about GemBox.Spreadsheet and GemBox Software.