How to Set Excel Page (Page Setup) with C#

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.

Detailed Steps:

  1. 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.
  2. set page orientation as portrait and paper size as A4.
  3. Insert page break by using sheet.VPageBreaks.Add() and sheet.HPageBreaks.Add() method. For the two methods, one parameter, data range is passed.
  4. Save and launch file.

Main Coding

using System;
using Spire.Xls;

namespace Page_Setup
{
    class Setup
    {
        static void Main(string[] args)
        {

            //Initailize worksheet
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"D:\Work\My Documents\Student Information.xlsx");
            Worksheet sheet= workbook.Worksheets[0];
            sheet.GridLinesVisible = false;

            //Sets page setup properties
            sheet.PageSetup.Orientation = PageOrientationType.Portrait;
            sheet.PageSetup.PaperSize = PaperSizeType.PaperA4;

            //Sets page break
            sheet.VPageBreaks.Add(sheet.Range["A8"]);
            sheet.HPageBreaks.Add(sheet.Range["D9"]);
            workbook.SaveToFile("PageSetup.xlsx",ExcelVersion.Version2010);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

Result:

 Freely Download Spire.XLS for .NET

Advertisements

One thought on “How to Set Excel Page (Page Setup) with C#

  1. Green products says:

    I leave a response each time I appreciate a post on a site or
    I have something to add to the discussion. Usually it is caused by
    the passion displayed in the post I read. And on this post How to Set Excel Page (Page Setup) with C# |
    janewdaisy. I was excited enough to create a comment 😉
    I do have a couple of questions for you if it’s allright.
    Is it just me or do some of the comments appear like left by
    brain dead individuals? 😛 And, if you are writing at other places, I would like
    to follow you. Could you list all of all your social sites like your Facebook page, twitter feed, or linkedin profile?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s