Print Excel in C# and VB.NET

The following sample demonstrates how to print an entire Excel file in C# and VB.NET using GemBox.Spreadsheet with the default and advanced print options specified via WPF's PrintDialog box.

Note that you can print to the default printer or to any other local or networked printer.

Screenshot

Print Screenshot

See the full code below.

<Window x:Class="PrintCs.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Printing" Height="600" Width="800">
    <DockPanel>
         <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="2">
            <Button x:Name="LoadFileBtn" Content="Load file" Width="100" Margin="2,0" Click="LoadFileBtn_Click"/>
            <Button x:Name="SimplePrintFileBtn" Content="Print" Width="65" Click="SimplePrint_Click" Margin="2,0"/>
            <Button x:Name="AdvancedPrintFileBtn" Content="Print (select options)" Width="130" Click="AdvancedPrint_Click" Margin="2,0"/>
         </StackPanel>
        <DocumentViewer x:Name="DocViewer"/>
    </DockPanel>
</Window>
1using System.Windows;
2using System.Windows.Controls;
3using System.Windows.Xps.Packaging;
4using GemBox.Spreadsheet;
5using Microsoft.Win32;
6
7// This is very simple project so we won't use MVVM pattern, ICommand, etc. (but we can)
8namespace PrintCs
9{
10    public partial class MainWindow : Window
11    {
12        private ExcelFile ef;
13
14        public MainWindow()
15        {
16            SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
17
18            InitializeComponent();
19
20            this.EnableControls();
21        }
22
23        private void LoadFileBtn_Click(object sender, RoutedEventArgs e)
24        {
25            OpenFileDialog fileDialog = new OpenFileDialog();
26            fileDialog.Filter = "XLSX files (*.xlsx, *.xlsm, *.xltx, *.xltm)|*.xlsx;*.xlsm;*.xltx;*.xltm|XLS files (*.xls, *.xlt)|*.xls;*.xlt|ODS files (*.ods, *.ots)|*.ods;*.ots|CSV files (*.csv, *.tsv)|*.csv;*.tsv|HTML files (*.html, *.htm)|*.html;*.htm";
27
28            if (fileDialog.ShowDialog() == true)
29            {
30                this.ef = ExcelFile.Load(fileDialog.FileName);
31
32                this.ShowPrintPreview();
33                this.EnableControls();
34            }
35        }
36
37        private void SimplePrint_Click(object sender, RoutedEventArgs e)
38        {
39            // Print to default printer using default options
40            this.ef.Print();
41        }
42
43        private void AdvancedPrint_Click(object sender, RoutedEventArgs e)
44        {
45            // We can use PrintDialog for defining print options
46            PrintDialog printDialog = new PrintDialog();
47            printDialog.UserPageRangeEnabled = true;
48
49            if (printDialog.ShowDialog() == true)
50            {
51                PrintOptions printOptions = new PrintOptions(printDialog.PrintTicket.GetXmlStream());
52
53                printOptions.FromPage = printDialog.PageRange.PageFrom - 1;
54                printOptions.ToPage = printDialog.PageRange.PageTo == 0 ? int.MaxValue : printDialog.PageRange.PageTo - 1;
55
56                this.ef.Print(printDialog.PrintQueue.FullName, printOptions);
57            }
58        }
59
60        // We can use DocumentViewer for print preview (but we don't need).
61        private void ShowPrintPreview()
62        {
63            // XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
64            // Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
65            XpsDocument xpsDocument = ef.ConvertToXpsDocument(SaveOptions.XpsDefault);
66            this.DocViewer.Tag = xpsDocument;
67
68            this.DocViewer.Document = xpsDocument.GetFixedDocumentSequence();
69        }  
70
71        private void EnableControls()
72        {
73            bool isEnabled = this.ef != null;
74
75            this.DocViewer.IsEnabled = isEnabled;
76            this.SimplePrintFileBtn.IsEnabled = isEnabled;
77            this.AdvancedPrintFileBtn.IsEnabled = isEnabled;
78        }
79    }
80}
1Imports Microsoft.Win32
2Imports GemBox.Spreadsheet
3
4' This is very simple project so we won't use MVVM pattern, ICommand, etc. (but we can)
5Class MainWindow
6
7    Dim ef As ExcelFile
8
9    Public Sub New()
10        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
11
12        InitializeComponent()
13
14        Me.EnableControls()
15    End Sub
16
17    Private Sub LoadFileBtn_Click(sender As Object, e As RoutedEventArgs)
18
19        Dim fileDialog = New OpenFileDialog()
20        fileDialog.Filter = "XLSX files (*.xlsx, *.xlsm, *.xltx, *.xltm)|*.xlsx;*.xlsm;*.xltx;*.xltm|XLS files (*.xls, *.xlt)|*.xls;*.xlt|ODS files (*.ods, *.ots)|*.ods;*.ots|CSV files (*.csv, *.tsv)|*.csv;*.tsv|HTML files (*.html, *.htm)|*.html;*.htm"
21
22        If (fileDialog.ShowDialog() = True) Then
23            Me.ef = ExcelFile.Load(fileDialog.FileName)
24
25            Me.ShowPrintPreview()
26            Me.EnableControls()
27        End If
28
29    End Sub
30
31    Private Sub SimplePrint_Click(sender As Object, e As RoutedEventArgs)
32
33        ' Print to default printer using default options
34        Me.ef.Print()
35
36    End Sub
37
38    Private Sub AdvancedPrint_Click(sender As Object, e As RoutedEventArgs)
39
40        ' We can use PrintDialog for defining print options
41        Dim printDialog = New PrintDialog()
42        printDialog.UserPageRangeEnabled = True
43
44        If (printDialog.ShowDialog() = True) Then
45
46            Dim printOptions = New PrintOptions(printDialog.PrintTicket.GetXmlStream())
47
48            printOptions.FromPage = printDialog.PageRange.PageFrom - 1
49            If (printDialog.PageRange.PageTo = 0) Then
50                printOptions.ToPage = Int32.MaxValue
51            Else
52                printOptions.ToPage = printDialog.PageRange.PageTo - 1
53            End If
54
55            Me.ef.Print(printDialog.PrintQueue.FullName, printOptions)
56        End If
57
58    End Sub
59
60    ' We can use DocumentViewer for print preview (but we don't need).
61    Private Sub ShowPrintPreview()
62
63        ' XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
64        ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
65        Dim xpsDocument = ef.ConvertToXpsDocument(SaveOptions.XpsDefault)
66        Me.DocViewer.Tag = xpsDocument
67
68        Me.DocViewer.Document = xpsDocument.GetFixedDocumentSequence()
69
70    End Sub
71
72    Private Sub EnableControls()
73
74        Dim isEnabled As Boolean = Me.ef IsNot Nothing
75
76        Me.DocViewer.IsEnabled = isEnabled
77        Me.SimplePrintFileBtn.IsEnabled = isEnabled
78        Me.AdvancedPrintFileBtn.IsEnabled = isEnabled
79
80    End Sub
81
82End Class

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