Java Apache POI - 在 Excel 中设置左/右打印边距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20966818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:37:22  来源:igfitidea点击:

Apache POI - setting left/right print margin in Excel

javaexcelapache-poi

提问by robson

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

是否可以 - 使用 apache POI - 为 Excel 工作表设置左侧或右侧打印边距?

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

默认边距相当大。我在 XSSFPrintSetup 中既看不到 setLeftMargin 也看不到 setRightMargin,只能看到页眉和页脚:

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

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

有没有好心的朋友可以帮帮我一点?

采纳答案by rgettman

The sheet margins are not contained in the XSSFPrintSetupobject, but on the XSSFSheetitself. Use Sheet's getMarginand setMarginmethods, passing the appropriate Sheetconstant for the top/left/bottom/right/header/footer margins. Set and get the margin in inches.

页边距不包含在XSSFPrintSetup对象中,而是包含在对象XSSFSheet本身中。使用Sheet'sgetMarginsetMargin方法,Sheet为顶部/左侧/底部/右侧/页眉/页脚边距传递适当的常量。以英寸为单位设置和获取边距。

double leftMarginInches = sheet.getMargin(Sheet.LeftMargin);
sheet.setMargin(Sheet.RightMargin, 0.5 /* inches */ );

回答by Timothy Dooling

The enumeration is now MarginType.LeftMargin, -RightMargin

枚举现在是 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.

这确实是NPOI。