How to set Excel margins with EPPlus

epplus, excel

Solution

I use epplus with this code for set printer settings in target excel file:

ExcelWorksheet ew;
ew.PrinterSettings.TopMargin = tartetTopMarginValueInCm / 2.54M;
ew.PrinterSettings.RightMargin = targetRightMarginValueInCm / 2.54M;
...
ew.PrinterSettings.HeaderMargin = targetHeaderMarginInCm / 2.54M;

Don't forget convert cm to inch (if you want use cm, because all epplus printer settings values are in inch).

Screen with Page setup in Excel vs. PrinterSettings in Epplus:

Problem

I need to be able to set the margins (printer settings) of an Excel workbook programmatically. I found this file: ExcelPrinterSettings.cs There is a class with following constructor: ``` ExcelPrinterSettings(XmlNamespaceManager ns, XmlNode topNode,ExcelWorksheet ws) ``` but I don't know what I should pass in for the first two parameters. I already had code that makes a worksheet, so I can pass that in as 3rd parameter. Many thanks for any suggestions.

Original source