1、引用Apache POI
使用Apache POI操作Word文件,需要在项目中包含了Apache POI的依赖。如使用Maven,可以在pom.xml
文件中添加以下依赖,如下,
<dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> </dependencies>
2、将Word文档分节
要在Java中使用Apache POI库将Word文档分节,可以通过在文档中插入分节符来实现。代码如下,
try (XWPFDocument document = new XWPFDocument()) { // 添加第一节内容 XWPFParagraph paragraph1 = document.createParagraph(); paragraph1.setAlignment(ParagraphAlignment.CENTER); XWPFRun run1 = paragraph1.createRun(); run1.setText("第一节内容"); //分节 CTBody body = document.getDocument().getBody(); CTSectPr sectPr = body.getSectPr(); if (sectPr != null) { XWPFParagraph lastParagraph = document.createParagraph(); lastParagraph.getCTP().addNewPPr().setSectPr(sectPr); body.unsetSectPr(); } document.createParagraph().setPageBreak(true); // 添加第二节内容 XWPFParagraph paragraph2 = document.createParagraph(); paragraph2.setAlignment(ParagraphAlignment.CENTER); XWPFRun run2 = paragraph2.createRun(); run2.setText("第二节内容"); // 创建新文档 //XWPFDocument document = new XWPFDocument(); // 保存文档 try (FileOutputStream out = new FileOutputStream("cjavapy.docx")) { document.write(out); } } catch (IOException e) { e.printStackTrace(); }
3、将Word文档设置页眉和页脚
可以使用Java POI库在Word文档中设置页眉和页脚样式,代码如下,
try (XWPFDocument document = new XWPFDocument()) { // 创建一个段落 XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText("www.cjavapy.com with header and footer."); // 设置页眉 XWPFHeaderFooterPolicy headerFooterPolicy = document.createHeaderFooterPolicy(); // 创建页眉内容 XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT); XWPFParagraph headerParagraph = header.createParagraph(); headerParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun headerRun = headerParagraph.createRun(); headerRun.setText("www.cjavapy.com header text"); // 设置页脚 XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT); XWPFParagraph footerParagraph = footer.createParagraph(); footerParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun footerRun = footerParagraph.createRun(); footerRun.setText("www.cjavapy.com footer text"); // 保存文档 try (FileOutputStream out = new FileOutputStream("cjavapy.docx")) { document.write(out); } } catch (IOException e) { e.printStackTrace(); }
参考文档:Java 使用POI操作Word的页眉和页脚的方法及示例代码
4、将Word文档分节设置页眉
使用Apache POI库可以编程方式操作Microsoft Word文档。要给Word文档的设置页眉,代码如下,
try (XWPFDocument document = new XWPFDocument()) { // 第一节内容 XWPFParagraph para1 = document.createParagraph(); XWPFRun run1 = para1.createRun(); run1.setText("www.cjavapy.com 第一节内容"); para1.setPageBreak(true); // 分页符 // 创建第一节的页眉 CTSectPr sectPr1 = document.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy headerFooterPolicy1 = new XWPFHeaderFooterPolicy(document, sectPr1); XWPFHeader header1 = headerFooterPolicy1.createHeader(XWPFHeaderFooterPolicy.DEFAULT); XWPFParagraph headerPara1 = header1.createParagraph(); headerPara1.setAlignment(ParagraphAlignment.CENTER); XWPFRun headerRun1 = headerPara1.createRun(); headerRun1.setText("cjavapy 页眉 1"); //分节 CTBody body = document.getDocument().getBody(); CTSectPr sectPr = body.getSectPr(); if (sectPr != null) { XWPFParagraph lastParagraph = document.createParagraph(); lastParagraph.getCTP().addNewPPr().setSectPr(sectPr); body.unsetSectPr(); } document.createParagraph().setPageBreak(true); // 第二节内容 XWPFParagraph para2 = document.createParagraph(); XWPFRun run2 = para2.createRun(); run2.setText("www.cjavapy.com 第二节内容"); // 创建第二节的页眉 CTSectPr sectPr2 = document.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy headerFooterPolicy2 = new XWPFHeaderFooterPolicy(document, sectPr2); XWPFHeader header2 = headerFooterPolicy2.createHeader(XWPFHeaderFooterPolicy.DEFAULT); XWPFParagraph headerPara2 = header2.createParagraph(); headerPara2.setAlignment(ParagraphAlignment.CENTER); XWPFRun headerRun2 = headerPara2.createRun(); headerRun2.setText("cjavapy 页眉 2"); // 保存文档 try (FileOutputStream out = new FileOutputStream("cjavapy.docx")) { document.write(out); } } catch (IOException e) { e.printStackTrace(); }