Java使用poi生成word文档的简单实例

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();
    }
}

运行,就可以看到效果了!!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WMSmile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值