您现在的位置是:首页 > 编程 > 

Java操作Word模板产生全新内容Word

2025-07-18 21:00:27
1. spire.doc的jar引用         首先我们需要用到国产word处理工具jar包spire.doc,可以通过maven仓库寻,然后在pom文件中直接引用。         此处需要注意,我们需要使用的是spire.doc.free(免费版的),切勿使用spire.doc(如果使

1. spire.doc的jar引用

        首先我们需要用到国产word处理工具jar包spire.doc,可以通过maven仓库寻,然后在pom文件中直接引用。

        此处需要注意,我们需要使用的是spire.doc.free(免费版的),切勿使用spire.doc(如果使用了,处理后的word文件第一页的顶部会出现红的警告水印信息)

        如果不能直接从仓库引用到此jar,可以在仓库直接下载下来后,手动存放与本地仓库中,处理方式详见本人的另一个帖子: 本地Maven仓库导入外部jar

2. 编辑模板内容,编辑后如下(示例,仅供参考):

 2.1 测试替换文字内容

/**
     * 替换文档中的制定文字
     *
     * @param inFilePath 文件存放全路径
     * @param map        要替换的内容(key为要替换的内容,value为要替换成的内容)
     * @return 产生的新文件存放地址
     * @throws FileotFoundException
     */
    public static String replaceText(String inFilePath, Map<String, String> map) throws FileotFoundException {
        Document doc = new Document(inFilePath);
//        doc.loadFromFile(inFilePath);
//        InputStream in = new BufferedInputStream(new FileInputStream(inFilePath));
//        doc.loadFromStream(in, FileFormat.Docx);
        map.forEach((k, v) -> {
            doc.replace(k, v, true, false);
        });
        String outFilePath = inFilePath.substring(0, inFilePath.lastIndexOf(.));
        outFilePath = _副本.docx;
        doc.saveToFile(outFilePath, FileFormat.Docx);
        return outFilePath;
    }

    public static void main(String[] args) throws FileotFoundException {
        String inFilePath = C:\\Users\\DaiHaijiao\\Desktop/aaa.docx;
        Map<String, String> map = new HashMap<>(8);
        map.put(${name}, 张三);
        map.put(${car}, 配送员李四(1521245678));
        map.put(${goodame}, 65寸小米电视机);
        map.put(${ordero}, O.8641854741);
        map.put(${dateTime}, 2022-05-06 16:25:0);
        String outPath = WordUtils.replaceText(inFilePath, map);
        println(outPath);
    }

执行完后会输出新文件地址路径:

 打开此文件我们可以看到内容已经替换完成了

 2.2 替换图片(此处以替换那个假公章为例)

/**
     * 替换文档中的第一张图片
     *
     * @param inFilePath 文档地址路径
     * @param imgPath    新图片地址
     * @return 产生的新文件存放地址
     * @throws FileotFoundException
     */
    public static String replaceOneImg(String inFilePath, String imgPath) throws FileotFoundException {
        Document doc = new Document(inFilePath);
        SectionCollection secti = doc.getSecti();
        boolean bool = false;
        for (int i = 0; i < secti.getCount(); i) {
            if (bool) {
                break;
            }
            Section section = secti.get(i);
            ParagraphCollection paragraphs = section.getParagraphs();
            for (int j = 0; j < paragraphs.getCount(); j) {
                if (bool) {
                    break;
                }
                DocumentObjectCollection childObjects = paragraphs.get(j).getChildObjects();
                for (int k = 0; k < childObjects.getCount(); k) {
                    Object obj = childObjects.get(k);
                    if (obj instanceof DocPicture) {
                        DocPicture pic = (DocPicture) obj;
                        pic.loadImage(imgPath);
                        bool = true;
                        break;
                    }
                }
            }
        }
        String outFilePath = inFilePath.substring(0, inFilePath.lastIndexOf(.));
        outFilePath = _副本.docx;
        doc.saveToFile(outFilePath, FileFormat.Docx);
        return outFilePath;
    }

    public static void main(String[] args) throws FileotFoundException {
        String inFilePath = C:\\Users\\DaiHaijiao\\Desktop/aaa.docx;
        String imgPath = C:\\Users\\DaiHaijiao\\Pictures/gz.png;
        String outPath = WordUtils.replaceOneImg(inFilePath, imgPath);
        println(outPath);
    }

执行完后打开产生的新文件,如下图:

 可以看到,原先的那个假公章已经被替换了,由于代码中的main方法没有替换相应的标识内容,所有那些并没有被修改。代码中写的是替换第一张图片(具体根据自己业务适当变通一下)

2. 替换文档中所有指定的相同文字成指定图片,替换前的模板如下图所示:

/**
     * 替换文档中所有指定的相同文字成指定图片
     *
     * @param inFilePath 文档地址路径
     * @param text       要替换的文字
     * @param imgPath    要替换成的图片路径
     * @return 产生的新文件存放地址
     * @throws FileotFoundException
     */
    public static String replaceText2Img(String inFilePath, String text, String imgPath) throws FileotFoundException {
        Document doc = new Document(inFilePath);

        TextSelection[] allString = doc.findAllString(text, true, false);
        int index;
        for (TextSelection textSelection : allString) {
            DocPicture pic = new DocPicture(doc);
            pic.loadImage(imgPath);
            TextRange range = textSelection.getAsOneRange();
            index = range.getOwnerParagraph().getChildObjects().indexOf(range);
            range.getOwnerParagraph().getChildObjects().insert(index, pic);
            range.getOwnerParagraph().getChildObjects().remove(range);
        }
        String outFilePath = inFilePath.substring(0, inFilePath.lastIndexOf(.));
        outFilePath = _副本.docx;
        doc.saveToFile(outFilePath, FileFormat.Docx);
        return outFilePath;
    }

    public static void main(String[] args) throws FileotFoundException {
        String inFilePath = C:\\Users\\DaiHaijiao\\Desktop/aaa.docx;
        String imgPath = C:\\Users\\DaiHaijiao\\Pictures/mf.png;
        String text = ${thisImg};
        WordUtils.replaceText2Img(inFilePath, text, imgPath);
    }

 替换后如下:

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1098592.html

相关标签:无
上传时间: 2025-07-15 09:35:29
留言与评论(共有 15 条评论)
本站网友 新圩二手房
17分钟前 发表
v) -> { doc.replace(k
本站网友 foursquare
27分钟前 发表
false); }); String outFilePath = inFilePath.substring(0
本站网友 印度海娜花曝光
17分钟前 发表
inFilePath.lastIndexOf(.)); outFilePath = _副本.docx; doc.saveToFile(outFilePath
本站网友 过期啤酒的妙用
8分钟前 发表
Map<String
本站网友 赵延勇
10分钟前 发表
0); String outPath = WordUtils.replaceText(inFilePath
本站网友 丁永宁
11分钟前 发表
v) -> { doc.replace(k
本站网友 官能
17分钟前 发表
\\Users\\DaiHaijiao\\Desktop/aaa.docx; Map<String
本站网友 宫城良田
15分钟前 发表
FileFormat.Docx); map.forEach((k
本站网友 崂山二手房出售
2分钟前 发表
2022-05-06 16
本站网友 高宁
9分钟前 发表
v
本站网友 防辐射食物
27分钟前 发表
imgPath); }  替换后如下:
本站网友 梦见自己生了个儿子是什么意思
0秒前 发表
v) -> { doc.replace(k
本站网友 萨拉赫
0秒前 发表
FileFormat.Docx); return outFilePath; } public static void main(String[] args) throws FileotFoundException { String inFilePath = C
本站网友 开开网
15分钟前 发表
map); println(outPath); } 执行完后会输出新文件地址路径:  打开此文件我们可以看到内容已经替换完成了  2.2 替换图片(此处以替换那个假公章为例) /** * 替换文档中的第一张图片 * * @param inFilePath 文档地址路径 * @param imgPath 新图片地址 * @return 产生的新文件存放地址 * @throws FileotFoundException */ public static String replaceOneImg(String inFilePath