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 Excel 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
7namespace PrintCs
8{
9    public partial class MainWindow : Window
10    {
11        private ExcelFile ef;
12
13        public MainWindow()
14        {
15            SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
16
17            InitializeComponent();
18
19            this.EnableControls();
20        }
21
22        private void LoadFileBtn_Click(object sender, RoutedEventArgs e)
23        {
24            OpenFileDialog fileDialog = new OpenFileDialog();
25            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";
26
27            if (fileDialog.ShowDialog() == true)
28            {
29                this.ef = ExcelFile.Load(fileDialog.FileName);
30
31                this.ShowPrintPreview();
32                this.EnableControls();
33            }
34        }
35
36        private void SimplePrint_Click(object sender, RoutedEventArgs e)
37        {
38            // Print to default printer using default options
39            this.ef.Print();
40        }
41
42        private void AdvancedPrint_Click(object sender, RoutedEventArgs e)
43        {
44            // We can use PrintDialog for defining print options
45            PrintDialog printDialog = new PrintDialog();
46            printDialog.UserPageRangeEnabled = true;
47
48            if (printDialog.ShowDialog() == true)
49            {
50                PrintOptions printOptions = new PrintOptions(printDialog.PrintTicket.GetXmlStream());
51
52                printOptions.FromPage = printDialog.PageRange.PageFrom - 1;
53                printOptions.ToPage = printDialog.PageRange.PageTo == 0 ? int.MaxValue : printDialog.PageRange.PageTo - 1;
54
55                this.ef.Print(printDialog.PrintQueue.FullName, printOptions);
56            }
57        }
58
59        // We can use DocumentViewer for print preview (but we don't need).
60        private void ShowPrintPreview()
61        {
62            // XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
63            // Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
64            XpsDocument xpsDocument = ef.ConvertToXpsDocument(SaveOptions.XpsDefault);
65            this.DocViewer.Tag = xpsDocument;
66
67            this.DocViewer.Document = xpsDocument.GetFixedDocumentSequence();
68        }  
69
70        private void EnableControls()
71        {
72            bool isEnabled = this.ef != null;
73
74            this.DocViewer.IsEnabled = isEnabled;
75            this.SimplePrintFileBtn.IsEnabled = isEnabled;
76            this.AdvancedPrintFileBtn.IsEnabled = isEnabled;
77        }
78    }
79}
1Imports Microsoft.Win32
2Imports GemBox.Spreadsheet
3
4Class MainWindow
5
6    Dim ef As ExcelFile
7
8    Public Sub New()
9        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
10
11        InitializeComponent()
12
13        Me.EnableControls()
14    End Sub
15
16    Private Sub LoadFileBtn_Click(sender As Object, e As RoutedEventArgs)
17
18        Dim fileDialog = New OpenFileDialog()
19        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"
20
21        If (fileDialog.ShowDialog() = True) Then
22            Me.ef = ExcelFile.Load(fileDialog.FileName)
23
24            Me.ShowPrintPreview()
25            Me.EnableControls()
26        End If
27
28    End Sub
29
30    Private Sub SimplePrint_Click(sender As Object, e As RoutedEventArgs)
31
32        ' Print to default printer using default options
33        Me.ef.Print()
34
35    End Sub
36
37    Private Sub AdvancedPrint_Click(sender As Object, e As RoutedEventArgs)
38
39        ' We can use PrintDialog for defining print options
40        Dim printDialog = New PrintDialog()
41        printDialog.UserPageRangeEnabled = True
42
43        If (printDialog.ShowDialog() = True) Then
44
45            Dim printOptions = New PrintOptions(printDialog.PrintTicket.GetXmlStream())
46
47            printOptions.FromPage = printDialog.PageRange.PageFrom - 1
48            If (printDialog.PageRange.PageTo = 0) Then
49                printOptions.ToPage = Int32.MaxValue
50            Else
51                printOptions.ToPage = printDialog.PageRange.PageTo - 1
52            End If
53
54            Me.ef.Print(printDialog.PrintQueue.FullName, printOptions)
55        End If
56
57    End Sub
58
59    ' We can use DocumentViewer for print preview (but we don't need).
60    Private Sub ShowPrintPreview()
61
62        ' XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
63        ' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
64        Dim xpsDocument = ef.ConvertToXpsDocument(SaveOptions.XpsDefault)
65        Me.DocViewer.Tag = xpsDocument
66
67        Me.DocViewer.Document = xpsDocument.GetFixedDocumentSequence()
68
69    End Sub
70
71    Private Sub EnableControls()
72
73        Dim isEnabled As Boolean = Me.ef IsNot Nothing
74
75        Me.DocViewer.IsEnabled = isEnabled
76        Me.SimplePrintFileBtn.IsEnabled = isEnabled
77        Me.AdvancedPrintFileBtn.IsEnabled = isEnabled
78
79    End Sub
80
81End Class

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