Maven引入依賴:apache
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
注:poi-ooxml是CTPageMar類的依賴包
1、處理已有文檔:佈局
// word標準佈局的頁邊距 private static long LEFT_MARGIN = 1800L; private static long RIGHT_MARGIN = 1800L; private static long TOP_MARGIN = 1440L; private static long BOTTOM_MARGIN = 1440L; CTSectPr sectPr = document.getDocument().getBody().getSectPr(); CTPageMar pageMar = sectPr.getPgMar(); pageMar.setLeft(BigInteger.valueOf(LEFT_MARGIN)); pageMar.setRight(BigInteger.valueOf(RIGHT_MARGIN)); pageMar.setTop(BigInteger.valueOf(TOP_MARGIN)); pageMar.setBottom(BigInteger.valueOf(BOTTOM_MARGIN));
2、處理新建文檔:spa
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); CTPageMar pageMar = sectPr.addNewPgMar(); pageMar.setLeft(BigInteger.valueOf(720L)); pageMar.setTop(BigInteger.valueOf(1440L)); pageMar.setRight(BigInteger.valueOf(720L)); pageMar.setBottom(BigInteger.valueOf(1440L));
-----------------------------------code