Use the Excel Export API to Set Page Margins

  • 2 minutes to read

The example below demonstrates how to set margins for a printed page using properties of the XlPageMargins object. This object provides access to the margin settings and allows you to specify the size of the top (XlPageMargins.Top), bottom (XlPageMargins.Bottom), left (XlPageMargins.Left), right (XlPageMargins.Right), header (XlPageMargins.Header) and footer (XlPageMargins.Footer) margins. By default, the margin size is set in inches. To change the unit of margin measurement, use the XlPageMargins.PageUnits property.

To set margins for a particular worksheet, assign an instance of the XlPageMargins class to the IXlSheet.PageMargins property.


The IXlSheet.PageMargins settings are used when worksheet pages are printed. To learn how to specify other print options for a worksheet, see the How to: Specify Print Settings document.

sheet.PageMargins = new XlPageMargins();
// Set the unit of margin measurement.
sheet.PageMargins.PageUnits = XlPageUnits.Centimeters;
// Specify page margins.
sheet.PageMargins.Left = 2.0;
sheet.PageMargins.Right = 1.0;
sheet.PageMargins.Top = 1.25;
sheet.PageMargins.Bottom = 1.25;
// Specify header and footer margins.
sheet.PageMargins.Header = 0.7;
sheet.PageMargins.Footer = 0.7;