Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache POI - setting left/right print margin in Excel

Is that possible - with apache POI - to set left or right print margin for Excel sheet?

The default margins are quite big. I cannot see neither setLeftMargin nor setRightMargin in XSSFPrintSetup, but only header and footer:

    XSSFPrintSetup printSetup = (XSSFPrintSetup) sheet.getPrintSetup();
    printSetup.setHeaderMargin(0.5D);
    printSetup.setFooterMargin(0.5D);

Is there any kind friend that could help me a little?

like image 442
robson Avatar asked Jan 07 '14 08:01

robson


People also ask

How do you fix left margin in Excel?

Use the Page Layout tab: On the Page Layout tab, click Margins, and then select Custom Margins.... Use the arrows to increase or decrease the margin sizes, or enter the desired size in the appropriate box. When you're done, click OK.

Why are my margins different when I print Excel?

This issue may occur if the printer driver uses the XML Paper Specification (XPS) PageScaling feature. When the driver uses this feature, Excel cannot determine the scale of the workbook.


1 Answers

The sheet margins are not contained in the XSSFPrintSetup object, but on the XSSFSheet itself. Use Sheet's getMargin and setMargin methods, passing the appropriate Sheet constant for the top/left/bottom/right/header/footer margins. Set and get the margin in inches.

double leftMarginInches = sheet.getMargin(Sheet.LeftMargin);
sheet.setMargin(Sheet.RightMargin, 0.5 /* inches */ );
like image 55
rgettman Avatar answered Oct 02 '22 01:10

rgettman