How to Set Excel Print Options in C#

Excel print options (also called as sheet options) allow users to control how worksheet pages are printed, such as set print paper size, print area, print titles, page order and so on. This article mainly discusses how developers set print options in C# by using Spire.XLS.

Here comes to the details of how developers configure print options in C#:

  • Download Spire.XLS for .NET (or Spire.Office for .NET) and install it on your system.
  • Add Spire.XLS.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.XLS\Bin\NET4.0\ Spire.XLS.dll".
  • You can use the class PageSetup to set the print options.

Set print paper size:

By default, the paper size is A4; you can set the PaperSize property of the worksheet to set the print paper size you desired.

//set print paper size as A3
sheet.PageSetup.PaperSize = PaperSizeType.PaperA3;

Set Print Area:

By default, the print area means all areas of the worksheet that contain data. You can set the PrintArea property of the worksheet to set the print area you want.

//set print area from cell "B2" to cell "F8"
sheet.PageSetup.PrintArea = "B2:F8";

Set Print Titles:

Spire.XLS allows you to designate row and column headers to repeat on all pages of a printed worksheet. To do so, use the PageSetup class' PrintTitleColumns and PrintTitleRows properties.

//Set column numbers A & B as title columns
sheet.PageSetup.PrintTitleColumns = "$A:$B";

//Set row numbers 1 & 2 as title rows
sheet.PageSetup.PrintTitleRows = "$1:$2";

Set Page Order:

The PageSetup class provides the Order property that is used to order multiple pages of your worksheet to be printed. There are two possibilities to order the pages as follows:

//set page order from down then over
sheet.PageSetup.Order = OrderType.DownThenOver;

//set page order from over then down
sheet.PageSetup.Order = OrderType.OverThenDown;

Below picture shows the Microsoft Excel's page print options:

Spire.xls for printing PageSetup