Print Excel files in C# and VB.NET
The following example shows how you can 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 network printer.

<Window x:Class="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>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Xps.Packaging;
using GemBox.Spreadsheet;
using Microsoft.Win32;
partial class MainWindow : Window
{
private ExcelFile workbook;
public MainWindow()
{
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
InitializeComponent();
this.EnableControls();
}
private void LoadFileBtn_Click(object sender, RoutedEventArgs e)
{
var fileDialog = new OpenFileDialog();
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";
if (fileDialog.ShowDialog() == true)
{
this.workbook = ExcelFile.Load(fileDialog.FileName);
this.ShowPrintPreview();
this.EnableControls();
}
}
private void SimplePrint_Click(object sender, RoutedEventArgs e)
{
// Print to default printer using default options
this.workbook.Print();
}
private void AdvancedPrint_Click(object sender, RoutedEventArgs e)
{
// We can use PrintDialog for defining print options
var printDialog = new PrintDialog();
printDialog.UserPageRangeEnabled = true;
if (printDialog.ShowDialog() == true)
{
var printOptions = new PrintOptions(printDialog.PrintTicket.GetXmlStream());
printOptions.FromPage = printDialog.PageRange.PageFrom - 1;
printOptions.ToPage = printDialog.PageRange.PageTo == 0 ? int.MaxValue : printDialog.PageRange.PageTo - 1;
this.workbook.Print(printDialog.PrintQueue.FullName, printOptions);
}
}
// We can use DocumentViewer for print preview (but we don't need).
private void ShowPrintPreview()
{
// XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
// Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
var xpsDocument = workbook.ConvertToXpsDocument(SaveOptions.XpsDefault);
this.DocViewer.Tag = xpsDocument;
this.DocViewer.Document = xpsDocument.GetFixedDocumentSequence();
}
private void EnableControls()
{
var isEnabled = this.workbook != null;
this.DocViewer.IsEnabled = isEnabled;
this.SimplePrintFileBtn.IsEnabled = isEnabled;
this.AdvancedPrintFileBtn.IsEnabled = isEnabled;
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Xps.Packaging
Imports GemBox.Spreadsheet
Imports Microsoft.Win32
Class MainWindow
Dim workbook As ExcelFile
Public Sub New()
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
InitializeComponent()
Me.EnableControls()
End Sub
Private Sub LoadFileBtn_Click(sender As Object, e As RoutedEventArgs)
Dim fileDialog = New OpenFileDialog()
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"
If (fileDialog.ShowDialog() = True) Then
Me.workbook = ExcelFile.Load(fileDialog.FileName)
Me.ShowPrintPreview()
Me.EnableControls()
End If
End Sub
Private Sub SimplePrint_Click(sender As Object, e As RoutedEventArgs)
' Print to default printer using default options
Me.workbook.Print()
End Sub
Private Sub AdvancedPrint_Click(sender As Object, e As RoutedEventArgs)
' We can use PrintDialog for defining print options
Dim printDialog = New PrintDialog()
printDialog.UserPageRangeEnabled = True
If (printDialog.ShowDialog() = True) Then
Dim printOptions = New PrintOptions(printDialog.PrintTicket.GetXmlStream())
printOptions.FromPage = printDialog.PageRange.PageFrom - 1
If (printDialog.PageRange.PageTo = 0) Then
printOptions.ToPage = Int32.MaxValue
Else
printOptions.ToPage = printDialog.PageRange.PageTo - 1
End If
Me.workbook.Print(printDialog.PrintQueue.FullName, printOptions)
End If
End Sub
' We can use DocumentViewer for print preview (but we don't need).
Private Sub ShowPrintPreview()
' XpsDocument needs to stay referenced so that DocumentViewer can access additional required resources.
' Otherwise, GC will collect/dispose XpsDocument and DocumentViewer will not work.
Dim xpsDocument = workbook.ConvertToXpsDocument(SaveOptions.XpsDefault)
Me.DocViewer.Tag = xpsDocument
Me.DocViewer.Document = xpsDocument.GetFixedDocumentSequence()
End Sub
Private Sub EnableControls()
Dim isEnabled = Me.workbook IsNot Nothing
Me.DocViewer.IsEnabled = isEnabled
Me.SimplePrintFileBtn.IsEnabled = isEnabled
Me.AdvancedPrintFileBtn.IsEnabled = isEnabled
End Sub
End Class