Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Excel margins with EPPlus

Tags:

excel

epplus

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.

like image 949
user2943111 Avatar asked Jan 09 '14 13:01

user2943111


1 Answers

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:

Page setup in excel vs. epplus settings

like image 151
Atiris Avatar answered Oct 21 '22 14:10

Atiris