当前位置: 代码网 > it编程>编程语言>Java > Java使用poi生成word文档的简单实例

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

2024年07月02日 Java 我要评论
java使用poi生成word文档的简单实例生成的效果如下:用到的poi的简单的知识新建一个word对象//新建文件xwpfdocument document = new xwpfdocument()

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

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

到此这篇关于java使用poi生成word文档的简单实例的文章就介绍到这了,更多相关java poi生成word文档内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com