Click or drag to resize

Worksheet Page Setup from C#/VB.NET

The ExpertXls Excel Library for .NET offers advanced support for setting the worksheet page. You can set the page size, orientation and margins, add headers and footers with text and images. The worksheet page properties can be accessed by the PageSetup property of the ExcelWorksheet class.

Code Samples

The code sample below shows how the worksheet page properties can be set:

// set worksheet paper size and orientation, margins, header and footer
worksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4;
worksheet.PageSetup.Orientation = ExcelPageOrientation.Portrait;
worksheet.PageSetup.LeftMargin = 1;
worksheet.PageSetup.RightMargin = 1;
worksheet.PageSetup.TopMargin = 1;
worksheet.PageSetup.BottomMargin = 1;

// add header and footer

//display a logo image in the left part of the header
string imagesPath = System.IO.Path.Combine(Server.MapPath("~"), @"Images");
System.Drawing.Image logoImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "logo.jpg"));
worksheet.PageSetup.LeftHeaderFormat = "&G";
worksheet.PageSetup.LeftHeaderPicture = logoImg;
// display worksheet name in the right part of the header
worksheet.PageSetup.RightHeaderFormat = "&A";

// add worksheet header and footer
// display the page number in the center part of the footer
worksheet.PageSetup.CenterFooterFormat = "&P";
// display the workbook file name in the left part of the footer
worksheet.PageSetup.LeftFooterFormat = "&F";
// display the current date in the right part of the footer
worksheet.PageSetup.RightFooterFormat = "&D";