Export Excel to XpsDocument in WPF

Following example shows how to convert an Excel workbook to XpsDocument instance in C# and VB.NET, and attach it to WPF's DocumentViewer control.

Screenshot
Export to XpsDocument / DocumentViewer Control Screenshot

See the full code below.

<Window x:Class="ConvertToXpsDocumentCs.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Convert to XpsDocument Sample" 
        SizeToContent="WidthAndHeight">

    <DocumentViewer x:Name="DocumentViewer"/>

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

namespace ConvertToXpsDocumentCs
{
    public partial class MainWindow : Window
    {
        XpsDocument xpsDocument;

        public MainWindow()
        {
            InitializeComponent();

            SetDocumentViewer(this.DocumentViewer);
        }

        private void SetDocumentViewer(DocumentViewer documentViewer)
        {
            SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

            ExcelFile ef = new ExcelFile();

            var ws = ef.Worksheets.Add("Sheet1");

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

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

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

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

            ws.HeadersFooters.DefaultPage.Header.CenterSection.Content = "Export To XpsDocument / DocumentViewer Control Sample";

            ws.PrintOptions.PrintGridlines = true;

            // XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
            // Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
            this.xpsDocument = ef.ConvertToXpsDocument(SaveOptions.XpsDefault);

            documentViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
        }
    }
}
Imports System.Windows.Controls
Imports System.Windows.Xps.Packaging
Imports GemBox.Spreadsheet

Class MainWindow

    Dim xpsDocument As XpsDocument

    Public Sub New()
        InitializeComponent()

        SetDocumentViewer(Me.DocumentViewer)
    End Sub

    Private Sub SetDocumentViewer(documentViewer As DocumentViewer)

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

        Dim ef As New ExcelFile()

        Dim ws = ef.Worksheets.Add("Sheet1")

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

        ws.Cells(1, 0).Value = "Russian:"
        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)})

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

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

        ws.HeadersFooters.DefaultPage.Header.CenterSection.Content = "Export To XpsDocument / DocumentViewer Control Sample"

        ws.PrintOptions.PrintGridlines = True

        ' XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
        ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
        xpsDocument = ef.ConvertToXpsDocument(SaveOptions.XpsDefault)

        documentViewer.Document = xpsDocument.GetFixedDocumentSequence()

    End Sub

End Class