Java使用poi生成word文档的简单实例
生成的效果如下:
用到的poi的简单的知识
新建一个word对象
//新建文件
XWPFDocument document = new XWPFDocument();
新建段落以及文字样式
//创建段落
XWPFParagraph paragraph = document.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);//创建标题
XWPFRun titleFun = paragraph.createRun();
titleFun.setText("个人信息表");
titleFun.setBold(true);
titleFun.setFontSize(25);
titleFun.setColor("000000");
titleFun.setFontFamily("宋体");
titleFun.addBreak();
titleFun.addBreak(); 换行
实例-生成一个简单word文档
新建一个maven项目
pom.xml 导入5.2.5的poi的依赖包
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.5</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.5</version></dependency>
新建SimpleWord.java文件
文件内容如下:
package com.wumeng.wordexport;import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;/*** @author wumeng 2024/6/27 17:56*/
public class SimpleWord {/*** 新建word* @throws IOException*/static void newWord(String outputPath) throws IOException {//新建文件XWPFDocument document = new XWPFDocument();//创建段落XWPFParagraph paragraph = document.createParagraph();paragraph.setAlignment(ParagraphAlignment.CENTER);//创建标题XWPFRun titleFun = paragraph.createRun();titleFun.setText("个人信息表");titleFun.setBold(true);titleFun.setFontSize(25);titleFun.setColor("000000");titleFun.setFontFamily("宋体");titleFun.addBreak();//添加引言XWPFParagraph paragraph2 = document.createParagraph();paragraph2.setAlignment(ParagraphAlignment.CENTER);XWPFRun titleFun2 = paragraph2.createRun();titleFun2.setText("引言");titleFun2.setBold(true);titleFun2.setFontSize(20);titleFun2.setColor("000000");titleFun2.setFontFamily("宋体");titleFun2.addBreak();//添加第一段内容XWPFParagraph paragraph3 = document.createParagraph();paragraph3.setAlignment(ParagraphAlignment.LEFT);XWPFRun titleFun3 = paragraph3.createRun();titleFun3.setText("个人信息表");titleFun3.setBold(true);titleFun3.setFontSize(14);titleFun3.setColor("000000");titleFun3.setFontFamily("宋体");XWPFRun titleFun4 = paragraph3.createRun();titleFun4.setText(",(注:以下内容请根据个人情况如实填写)");titleFun4.setFontSize(14);titleFun4.setColor("000000");titleFun4.setFontFamily("宋体");titleFun4.addBreak();//创建表格XWPFTable table = document.createTable(1,3);SimpleWordUtil.setTableWidthAndHAlign(table, "9072",STJcTable.Enum.forString("center"));List<String> headers = new ArrayList<String>();headers.add("姓名");headers.add("性别");headers.add("年龄");for (int i = 0; i < headers.size(); i++) {XWPFTableCell cell = table.getRow(0).getCell(i);XWPFParagraph cellParagraph = cell.getParagraphArray(0);cellParagraph.setAlignment(ParagraphAlignment.CENTER);XWPFRun cellParagraphRun = cellParagraph.createRun();cellParagraphRun.setFontSize(12);cellParagraphRun.setFontFamily("宋体");cellParagraphRun.setText(headers.get(i));cellParagraphRun.setBold(true);}List<List<String>> lists = new ArrayList<List<String>>();List<String> list1 = new ArrayList<String>();list1.add("黄xx");list1.add("男");list1.add("18");lists.add(list1);List<String> list2 = new ArrayList<String>();list2.add("王xx");list2.add("女");list2.add("16");lists.add(list2);for (int i = 0; i < lists.size(); i++) {table.createRow();for (int j = 0; j < lists.get(i).size(); j++) {XWPFTableCell cell = table.getRow(i+1).getCell(j);XWPFParagraph cellParagraph = cell.getParagraphArray(0);cellParagraph.setAlignment(ParagraphAlignment.CENTER);XWPFRun cellParagraphRun = cellParagraph.createRun();cellParagraphRun.setFontSize(12);cellParagraphRun.setFontFamily("宋体");cellParagraphRun.setText(lists.get(i).get(j));}}//保存文档SimpleWordUtil.saveDocument(document, outputPath);}public static void main(String[] args) throws Exception {newWord("./14.docx");}}
相关工具类SimpleWordUtil.java
package com.wumeng.wordexport;import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;/*** @author wumeng 2024/6/27 18:03*/
public class SimpleWordUtil {/*** 获取表格属性* @param table* @return*/public static CTTblPr getTableCTTblPr(XWPFTable table) {CTTbl ttbl = table.getCTTbl();// 表格属性CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();return tblPr;}/*** 设置表格宽度和水平对齐方式* @param table* @param width* @param enumValue*/public static void setTableWidthAndHAlign(XWPFTable table, String width,STJcTable.Enum enumValue) {CTTblPr tblPr = getTableCTTblPr(table);// 表格宽度CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();if (enumValue != null) {CTJcTable ctJcTable = tblPr.addNewJc();ctJcTable.setVal(enumValue);}// 设置宽度tblWidth.setW(new BigInteger(width));tblWidth.setType(STTblWidth.DXA);}/*** 保存文档* @param document* @param savePath* @throws IOException*/public static void saveDocument(XWPFDocument document, String savePath) throws IOException {OutputStream os = new FileOutputStream(savePath);document.write(os);os.close();}
}