1、安装引用Apache POI
Maven中pom.xml
中添加依赖如下:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
2、读取txt文件中内容
可以读取txt文本文件中内容插入word中。
public String convertTextFileToString(String fileName) {
try (Stream<String> stream
= Files.lines(Paths.get(ClassLoader.getSystemResource(fileName).toURI()))) {
return stream.collect(Collectors.joining(" "));
} catch (IOException | URISyntaxException e) {
return null;
}
}
3、格式化标题和子标题
创建标题,需要首先实例化XWPFParagraph类,并在新对象上设置对齐方式:
XWPFDocument document = new XWPFDocument(); XWPFParagraph title = document.createParagraph(); title.setAlignment(ParagraphAlignment.CENTER); //段落的内容需要包装在XWPFRun对象中,可以配置此对象以设置文本值及其关联的样式: XWPFRun titleRun = title.createRun(); titleRun.setText("Build Your REST API with Spring"); titleRun.setColor("009933"); titleRun.setBold(true); titleRun.setFontFamily("Courier"); titleRun.setFontSize(20); //创建一个XWPFParagraph实例,该实例包含子标题 XWPFParagraph subTitle = document.createParagraph(); subTitle.setAlignment(ParagraphAlignment.CENTER); XWPFRun subTitleRun = subTitle.createRun(); subTitleRun.setText("from HTTP fundamentals to API Mastery"); subTitleRun.setColor("00CC44"); subTitleRun.setFontFamily("Courier"); subTitleRun.setFontSize(16); subTitleRun.setTextPosition(20); subTitleRun.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
4、word中插入图片
图片也需要包装在XWPFParagraph实例中。要将图片水平居中放置在字幕下方,则需将下面代码段放在上面给出的代码下方:
XWPFParagraph image = document.createParagraph(); image.setAlignment(ParagraphAlignment.CENTER); //设置此图像与其下方文本之间距离的方法 XWPFRun imageRun = image.createRun(); imageRun.setTextPosition(20); //获取图像,然后将其插入具有指定尺寸的MS Word文件中 Path imagePath = Paths.get(ClassLoader.getSystemResource(logo).toURI()); imageRun.addPicture(Files.newInputStream(imagePath), XWPFDocument.PICTURE_TYPE_PNG, imagePath.getFileName().toString(), Units.toEMU(50), Units.toEMU(50));
5、通过txt文件中内容创建段落并设置格式
//通过txt中内容创建第一段 WPFParagraph para1 = document.createParagraph(); para1.setAlignment(ParagraphAlignment.BOTH); String string1 = convertTextFileToString("./paragraph1.txt"); XWPFRun para1Run = para1.createRun(); para1Run.setText(string1); //创建另外两个段落 XWPFParagraph para2 = document.createParagraph(); para2.setAlignment(ParagraphAlignment.RIGHT); String string2 = convertTextFileToString("./paragraph2.txt"); XWPFRun para2Run = para2.createRun(); para2Run.setText(string2); para2Run.setItalic(true); XWPFParagraph para3 = document.createParagraph(); para3.setAlignment(ParagraphAlignment.LEFT); String string3 = convertTextFileToString("./paragraph3.txt"); XWPFRun para3Run = para3.createRun(); para3Run.setText(string3);
6、创建生成word文件
//XWPFDocument document = new XWPFDocument(); FileOutputStream out = new FileOutputStream(output); document.write(out); out.close(); document.close();
7、word添加水印
//添加水印 public void createWaterMark(XWPFDocument doc){ XWPFHeaderFooterPolicy policy=doc.getHeaderFooterPolicy(); policy.createWatermark("中国华西监理信息管理平台"); }
相关文档:
java 使用aspose.word多张图片转成pdf的方法及示例代码
java利用aspose组件将word转成pdf 中文乱码问题