前期调研的itext,生成效果不行,版本太老,就不发出来了。
1.使用poi库生成
引入pom.xml
org.apache.poi
poi
4.1.0
org.apache.poi
poi-ooxml
4.1.0
生成文件代码
XWPFDocument document= new XWPFDocument();
//Write the Document in file system
FileOutputStream out = new FileOutputStream(new File("/Users/xxx/work/xxx/create_toc.docx"));
//添加标题
XWPFParagraph titleParagraph = document.createParagraph();
//设置段落居中
titleParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun titleParagraphRun = titleParagraph.createRun();
titleParagraphRun.setText("Java PoI");
titleParagraphRun.setColor("000000");
titleParagraphRun.setFontSize(20);
//段落
XWPFParagraph firstParagraph = document.createParagraph();
firstParagraph.getStyleID();
firstParagr