将值从Java变量传递到Microsoft Word(doc和docx)变量 [英] pass values from java variables to microsoft word (doc and docx) variables

查看:230
本文介绍了将值从Java变量传递到Microsoft Word(doc和docx)变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用Microsoft变量中的值替换Microsoft Word文档变量值?我有一个.doc或.docx文件模板,其中定义了一些变量.

How can i replace a microsoft word document variable value with a value from a java variable ? I have a .doc or .docx file template in which i have defined some variables.

当用户从我的应用程序中单击下载按钮时,.doc或.docx变量必须从java变量获取值.

When user click on download button from my app the .doc or .docx variables must get the value from java variables.

推荐答案

我为此目的使用docx4j:

I use docx4j for that purpose:

        String inputfilepath = "binding-simple1.docx";
        String outputfilepath = "OUT_VariableReplace.docx";

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
                .load(new java.io.File(inputfilepath));
        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

        HashMap<String, String> mappings = new HashMap<String, String>();
        mappings.put("subjectId", "E000001");

        // Approach 1 (from 3.0.0; faster if you haven't yet caused unmarshalling to occur):

        documentPart.variableReplace(mappings);
        Docx4J.save(wordMLPackage, new File(outputfilepath));

可变参数如下:${subjectId}

这篇关于将值从Java变量传递到Microsoft Word(doc和docx)变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆