Export Excel to XpsDocument in WPF

Following sample shows how to convert workbook to XpsDocument instance 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>
1using System.Windows;
2using System.Windows.Controls;
3using System.Windows.Xps.Packaging;
4using GemBox.Spreadsheet;
5
6namespace ConvertToXpsDocumentCs
7{
8    public partial class MainWindow : Window
9    {
10        XpsDocument xpsDocument;
11
12        public MainWindow()
13        {
14            InitializeComponent();
15
16            SetDocumentViewer(this.DocumentViewer);
17        }
18
19        private void SetDocumentViewer(DocumentViewer documentViewer)
20        {
21            SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
22
23            ExcelFile ef = new ExcelFile();
24
25            var ws = ef.Worksheets.Add("Sheet1");
26
27            ws.Cells[0, 0].Value = "English:";
28            ws.Cells[0, 1].Value = "Hello";
29
30            ws.Cells[1, 0].Value = "Russian:";
31            ws.Cells[1, 1].Value = new string(new char[] { '\u0417', '\u0434', '\u0440', '\u0430', '\u0432', '\u0441', '\u0442', '\u0432', '\u0443', '\u0439', '\u0442', '\u0435' });
32
33            ws.Cells[2, 0].Value = "Chinese:";
34            ws.Cells[2, 1].Value = new string(new char[] { '\u4f60', '\u597d' });
35
36            ws.Cells[4, 0].Value = "In order to see Russian and Chinese characters you need to have appropriate fonts on your PC.";
37            ws.Cells.GetSubrangeAbsolute(4, 0, 4, 7).Merged = true;
38
39            ws.HeadersFooters.DefaultPage.Header.CenterSection.Content = "Export To XpsDocument / DocumentViewer Control Sample";
40
41            ws.PrintOptions.PrintGridlines = true;
42
43            // XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
44            // Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
45            this.xpsDocument = ef.ConvertToXpsDocument(SaveOptions.XpsDefault);
46
47            documentViewer.Document = this.xpsDocument.GetFixedDocumentSequence();
48        }
49    }
50}
1Imports System.Windows.Controls
2Imports System.Windows.Xps.Packaging
3Imports GemBox.Spreadsheet
4
5Class MainWindow
6
7    Dim xpsDocument As XpsDocument
8
9    Public Sub New()
10        InitializeComponent()
11
12        SetDocumentViewer(Me.DocumentViewer)
13    End Sub
14
15    Private Sub SetDocumentViewer(documentViewer As DocumentViewer)
16
17        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
18
19        Dim ef As New ExcelFile()
20
21        Dim ws = ef.Worksheets.Add("Sheet1")
22
23        ws.Cells(0, 0).Value = "English:"
24        ws.Cells(0, 1).Value = "Hello"
25
26        ws.Cells(1, 0).Value = "Russian:"
27        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)})
28
29        ws.Cells(2, 0).Value = "Chinese:"
30        ws.Cells(2, 1).Value = New String(New Char() {ChrW(&H4F60), ChrW(&H597D)})
31
32        ws.Cells(4, 0).Value = "In order to see Russian and Chinese characters you need to have appropriate fonts on your PC."
33        ws.Cells.GetSubrangeAbsolute(4, 0, 4, 7).Merged = True
34
35        ws.HeadersFooters.DefaultPage.Header.CenterSection.Content = "Export To XpsDocument / DocumentViewer Control Sample"
36
37        ws.PrintOptions.PrintGridlines = True
38
39        ' XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
40        ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
41        xpsDocument = ef.ConvertToXpsDocument(SaveOptions.XpsDefault)
42
43        documentViewer.Document = xpsDocument.GetFixedDocumentSequence()
44
45    End Sub
46
47End Class

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