ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**1. 依赖包** ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` **2. 基本的word编写** ```java public void write01() throws IOException { //创建word文档对象 XWPFDocument docx = new XWPFDocument(); //创建段落 XWPFParagraph title = docx.createParagraph(); //设置段落水平居中 title.setAlignment(ParagraphAlignment.CENTER); //创建段落文本 XWPFRun titleRun = title.createRun(); //字体颜色 titleRun.setColor("000000"); //字体大小 titleRun.setFontSize(25); //字体名称 titleRun.setFontFamily("仿宋"); //true字体加粗 titleRun.setBold(true); //段落文本 titleRun.setText("主标题"); //换行 titleRun.addBreak(); //添加段落 XWPFParagraph firstTitle = docx.createParagraph(); firstTitle.setAlignment(ParagraphAlignment.LEFT); XWPFRun firstTitleRun = firstTitle.createRun(); firstTitleRun.setColor("000000"); firstTitleRun.setFontSize(13); firstTitleRun.setFontFamily("楷体"); firstTitleRun.setBold(true); firstTitleRun.setText("1.1 秋天美景"); firstTitleRun.addBreak(); //添加段落 XWPFParagraph firstParagraph = docx.createParagraph(); firstParagraph.setAlignment(ParagraphAlignment.LEFT); XWPFRun firstRun = firstTitle.createRun(); firstRun.setColor("000000"); firstRun.setFontSize(12); firstRun.setFontFamily("楷体"); firstRun.setBold(false); //缩进 firstRun.addTab(); firstRun.setText("金秋,天气越来越冷,树叶渐渐的变成黄色和红色的了。苹果树长了很多苹果。枣树长了很多枣。许多的树林都在金秋变成果园。"); firstRun.addBreak(); //缩进 firstRun.addTab(); firstRun.setText("秋风一闪,万树都会报以热烈的掌声和优美的舞姿,那个场面隆重盛大,不亚于盛大晚会的热闹场面,满天飞舞着蝶一般的落叶。"); firstRun.addBreak(); //word文档到磁盘 OutputStream out = new FileOutputStream("e:/upload/001.docx"); docx.write(out); out.flush(); out.close(); docx.close(); } ``` ![](https://img.kancloud.cn/f1/57/f1572aef2f93052346173f3f7a630801_1899x737.png)