C#/VB.NET - How to Print Excel Documents

Alexander Stock
5 min readOct 22, 2022

--

Excel document is one of the most frequently used documents in our daily life and work. It’s quite common that we may need to print Excel documents after creation, so that the data on the document can be viewed without using any electronic equipment. In this article, I am going to introduce how to programmatically print Excel documents in C# and VB.NET using Spire.XLS for .NET.

  • Set Excel Print Options via Page Setup
  • Print Excel Documents Using Print Dialog
  • Silently Print Excel Documents

Install Spire.XLS for .NET

Method #1: Download Spire.XLS and unzip the package somewhere on your disk to find the “BIN” folder. Spire.XLS has the DLLs compiled for multiple versions of .NET Framework as well as for .NET Core and other platforms. Choose the DLLs from the folder that you exactly need and add them all as dependencies in your project.

Method #2: Create a Windows Form application in you Visual Studio, and install Spire.XLS directly through NuGet. NuGet package manager will automatically install the correct version that fits in with your application.

Set Excel Print Options via Page Setup

Before you send an Excel file to printer, you can set the print options such as print area, page margins, and black and white mode through page setup. The following are the steps to set Excel print options through Excel page setup using Spire.XLS for .NET.

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet through Workbook.Worksheets[index] property.
  • Get PageSetup object through Worksheet.PageSetup property.
  • Set page margins, print area, pint title row, print quality, etc. through the properties under PageSetup object.
  • Save the workbook to another Excel file using Workbook.SaveToFile() method.

[C#]

using Spire.Xls;namespace PrintOptions
{
class Program
{
static void Main(string[] args)
{
//Create a workbook
Workbook workbook = new Workbook();
//Load an Excel document
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
//Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
//Get the PageSetup object of the first worksheet
PageSetup pageSetup = worksheet.PageSetup;
//Set page margins
pageSetup.TopMargin = 0.3;
pageSetup.BottomMargin = 0.3;
pageSetup.LeftMargin = 0.3;
pageSetup.RightMargin = 0.3;
//Specify print area
pageSetup.PrintArea = "A1:D10";
//Specify title row
pageSetup.PrintTitleRows = "$1:$2";
//Allow to print with row/column headings
pageSetup.IsPrintHeadings = true;
//Allow to print with gridlines
pageSetup.IsPrintGridlines = true;
//Allow to print comments as displayed on worksheet
pageSetup.PrintComments = PrintCommentType.InPlace;
//Set printing quality (dpi)
pageSetup.PrintQuality = 300;
//Allow to print worksheet in black & white mode
pageSetup.BlackAndWhite = true;
//Set the printing order
pageSetup.Order = OrderType.OverThenDown;
//Save the document to file
workbook.SaveToFile("PagePrintOptions.xlsx", ExcelVersion.Version2016);
}
}
}

[VB.NET]

Imports Spire.Xls

Namespace PrintOptions
Class Program
Shared Sub Main(ByVal args() As String)
'Create a workbook
Dim workbook As Workbook = New Workbook()

'Load an Excel document
workbook.LoadFromFile("C:\Users\Administrator\Desktop\sample.xlsx")

'Get the first worksheet
Dim worksheet As Worksheet = workbook.Worksheets(0)

'Get the PageSetup object of the first worksheet
Dim pageSetup As PageSetup = worksheet.PageSetup

'Set page margins
pageSetup.TopMargin = 0.3
pageSetup.BottomMargin = 0.3
pageSetup.LeftMargin = 0.3
pageSetup.RightMargin = 0.3

'Specify print area
pageSetup.PrintArea = "A1:D10"

'Specify title row
pageSetup.PrintTitleRows = "$1:$2"

'Allow to print with row/column headings
pageSetup.IsPrintHeadings = True

'Allow to print with gridlines
pageSetup.IsPrintGridlines = True

'Allow to print comments as displayed on worksheet
pageSetup.PrintComments = PrintCommentType.InPlace

'Set printing quality (dpi)
pageSetup.PrintQuality = 300

'Allow to print worksheet in black & white mode
pageSetup.BlackAndWhite = True

'Set the printing order
pageSetup.Order = OrderType.OverThenDown

'Save the document to file
workbook.SaveToFile("PagePrintOptions.xlsx", ExcelVersion.Version2016)
End Sub
End Class
End Namespace

Print Excel Documents Using Print Dialog

Below are the steps to print an Excel document using print dialog using Spire.XLS for .NET.

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Create a PrintDialog object.
  • Specify printer settings through the properties under PrintDialog object.
  • Apply the print dialog to workbook.
  • Create a PrintDocument object based on the workbook.
  • Invoke the print dialog.

[C#]

using System;
using Spire.Xls;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace PrintExcelUsingPrintDialog
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Create a Workbook object
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
//Create a PrintDialog object
PrintDialog dialog = new PrintDialog();
//Specify printer settings
dialog.AllowCurrentPage = true;
dialog.AllowSomePages = true;
dialog.AllowSelection = true;
dialog.UseEXDialog = true;
dialog.PrinterSettings.Duplex = Duplex.Simplex;
dialog.PrinterSettings.PrintRange = PrintRange.AllPages;

//Apply the dialog to workbook
workbook.PrintDialog = dialog;
//Create a PrintDocument object based on the workbook
PrintDocument printDocument = workbook.PrintDocument;
//Invoke the print dialog
if (dialog.ShowDialog() == DialogResult.OK)
{
printDocument.Print();
}
}
}
}

[VB.NET]

Imports System
Imports Spire.Xls
Imports System.Drawing.Printing
Imports System.Windows.Forms

Namespace PrintExcelUsingPrintDialog
Public partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
'Create a Workbook object
Dim workbook As Workbook = New Workbook()

'Load an Excel file
workbook.LoadFromFile("C:\Users\Administrator\Desktop\sample.xlsx")

'Create a PrintDialog object
Dim dialog As PrintDialog = New PrintDialog()

'Specify printer settings
dialog.AllowCurrentPage = True
dialog.AllowSomePages = True
dialog.AllowSelection = True
dialog.UseEXDialog = True
dialog.PrinterSettings.Duplex = Duplex.Simplex
dialog.PrinterSettings.PrintRange = PrintRange.AllPages

'Apply the dialog to workbook
workbook.PrintDialog = dialog

'Create a PrintDocument object based on the workbook
Dim printDocument As PrintDocument = workbook.PrintDocument

'Invoke the print dialog
If dialog.ShowDialog() = DialogResult.OK Then
printDocument.Print()
End If
End Sub
End Class
End Namespace

Silently Print Excel Documents

The following are the steps to silent print an Excel document using Spire.XLS for .NET.

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Set the print controller to StandardPrintController, which will prevent print process from showing.
  • Get PrinterSettings from the workbook
  • Specify printer name, duplex mode and print pages through the properties under PrinerSettings object.
  • Print the workbook using Workbook.PrintDocument.Print() method.

[C#]

using System.Drawing.Printing;
using Spire.Xls;
namespace SilentlyPrint
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook object
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
//Set the print controller to StandardPrintController, which will prevent print process from showing
workbook.PrintDocument.PrintController = new StandardPrintController();
//Get PrinterSettings from the workbook
PrinterSettings settings = workbook.PrintDocument.PrinterSettings;
//Specify printer name, duplex mode and print pages
settings.PrinterName = "HP LaserJet P1007";
settings.Duplex = Duplex.Simplex;
settings.FromPage = 1;
settings.ToPage = 3;
//Print the workbook
workbook.PrintDocument.Print();
}
}
}

[VB.NET]

Imports System.Drawing.Printing
Imports Spire.Xls

Namespace SilentlyPrint
Class Program
Shared Sub Main(ByVal args() As String)
'Create a Workbook object
Dim workbook As Workbook = New Workbook()

'Load an Excel file
workbook.LoadFromFile("C:\Users\Administrator\Desktop\sample.xlsx")

'Set the print controller to StandardPrintController, which will prevent print process from showing
workbook.PrintDocument.PrintController = New StandardPrintController()

'Get PrinterSettings from the workbook
Dim settings As PrinterSettings = workbook.PrintDocument.PrinterSettings

'Specify printer name, duplex mode and print pages
settings.PrinterName = "HP LaserJet P1007"
settings.Duplex = Duplex.Simplex
settings.FromPage = 1
settings.ToPage = 3

'Print the workbook
workbook.PrintDocument.Print()
End Sub
End Class
End Namespace

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.