简体   繁体   中英

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?

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 */ );

for Kotlin the following works:

    // set margins to 0.5 cm = 0.197 inches
    xlWSheet.setMargin(Sheet.LeftMargin, 0.197)
    xlWSheet.setMargin(Sheet.RightMargin, 0.197)
    xlWSheet.setMargin(Sheet.TopMargin, 0.197)
    xlWSheet.setMargin(Sheet.BottomMargin, 0.197)

The enumeration is now MarginType.LeftMargin, -RightMargin

double leftMargin = sheet.GetMargin(MarginType.LeftMargin);
double rightMargin = sheet.GetMargin(MarginType.RightMargin);

Update:

The code preceding this code is:

            var workbook = new XSSFWorkbook();
            var sheet = workbook.CreateSheet(sheetname);
            double leftMargin = sheet.GetMargin(MarginType.LeftMargin);
            double rightMargin = sheet.GetMargin(MarginType.RightMargin);

This is indeed the NPOI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM