将java变量中的值传递给microsoft word(doc和docx)变量

时间:2014-04-02 12:41:54

标签: java ms-word docx doc

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

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

2 个答案:

答案 0 :(得分:0)

我负责玩游戏!使用具有.docx扩展名的模板生成word文档的Intranet。为此,我们有以下继承树:Document> Word> [someDocument]

抽象类Word处理替换Word文档中的变量

public abstract class Word extends Document {
    public static JAXBContext context = org.docx4j.jaxb.Context.jc;

    public Word(String inputfilepath){
        super(inputfilepath);
    }

    public String generer(String outputfilepath) throws Exception {

        //String inputfilepath = System.getProperty("user.dir")+"/app/doc/adhesionTemplate.docx";

        //String outputfilepath = System.getProperty("user.dir")+ "/test-out.docx";

        // Open a document from the file system
        // 1. Load the Package
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));

        // 2. Fetch the document part
        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

        org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();

        // xml --> string
        String xml = XmlUtils.marshaltoString(wmlDocumentEl, true);

        //Change the variables using an abstract function getMapping()
        HashMap<String, String> mappings = getMapping();

        Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);

        // change JaxbElement
        documentPart.setJaxbElement((org.docx4j.wml.Document) obj);

        //Footers : 
        List<SectionWrapper> wrappers = wordMLPackage.getDocumentModel().getSections();
        for (SectionWrapper sw : wrappers) {
            FooterPart footer = sw.getHeaderFooterPolicy().getDefaultFooter();
            if (footer != null) {
                Ftr footerDoc = footer.getJaxbElement();
                String footerXml = XmlUtils.marshaltoString(footerDoc, true);
                Object footerObj = XmlUtils.unmarshallFromTemplate(footerXml, mappings);
                footer.setJaxbElement( (Ftr) footerObj);
            }
        }

        // Save it
        SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
        saver.save(outputfilepath);
        Console.commande("sudo chmod 660 \"" + outputfilepath + "\"");

        System.out.println("Saved output to:" + outputfilepath);

        return outputfilepath;

    }

然后,我们有从这个Word抽象类继承的类:

public class FAC extends Word {

    public FAC() {
        super(System.getProperty("user.dir") + "/templates/Facture.docx");
    }

    @Override
    public HashMap<String, String> getMapping() {

        //Preparing variables
        int price = blablabla;

        HashMap<String, String> map = new HashMap<String, String>();

        map.put("FACDate", Utils.dateConvert(new Date()));
        map.put("somePrice", String.valueOf(price));


        return map;
    }
}

注意:“Document”超类没什么特别的,只是变量“inputFilePath”,抽象方法getMapping()

希望这个帮助,无论是你还是未来的观众,比如我:P

答案 1 :(得分:0)

我为此目的使用docx4j:

        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}