Excel Page Setup is used to set page layout to have a good appearance after printing. It includes, margins, orientation, size, breaks and so on.
- Margin: Confirm location of table in paper.
- Orientation: Include Portrait and Landscape. Portrait means that table will be printed vertically, which will be used when rows are much more than columns. Landscape means that table will be printed horizontally, which will be used when columns are much more than columns.
- Size: Confirm appropriate printed paper size. The most frequently used paper size is A4.
- Breaks: Divide data information with several pages.
In this post, I want to introduce a method how to do on Excel Page Setup with C#.
Before starting with steps, I want to notice you that I use a C# .NET Excel component, Spire.XLS for .NET, a component to operate Excel for .NET and Silverlight, in this method and I have added its dll file as reference in my project.
- Load Excel Workbook which I want to set page from computer and initialize data worksheet. Then, don’t display grid lines for having a good look.
- set page orientation as portrait and paper size as A4.
- Insert page break by using sheet.VPageBreaks.Add() and sheet.HPageBreaks.Add() method. For the two methods, one parameter, data range is passed.
- Save and launch file.
workbook = new
workbook.LoadFromFile(@"D:\Work\My Documents\Student Information.xlsx"
sheet.GridLinesVisible = false
//Sets page setup properties
sheet.PageSetup.Orientation = PageOrientationType
sheet.PageSetup.PaperSize = PaperSizeType
//Sets page break