Velocity+docx4j 模板替换

http://kodak-zhou.iteye.com/blog/970682
http://velocity.apache.org/engine/devel/developer-guide.html
http://www.360doc.com/content/11/1203/22/834950_169480722.shtml
http://www.java2s.com/Code/Java/Velocity/VelocityMathToolAdd.htm

import java.io.File;
import java.io.StringWriter;
import java.util.Map;
import java.util.Properties;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Document;

public final class DocumentTemplateUtil {
/**
* Method to do the template merge directly in Ms Word and return the document/xml contents
* The format of replaced content is "$colour";
*
* @param sourceFile  the source file of .docx
* @param templateParams
*        the contents need to be merged  like:{( "color", "Red" )}
* @return A StringWriter contents word/document.xml's contents
*/

@SuppressWarnings("rawtypes")
public static StringWriter getMsWordDocumentXMLString(File sourceFile, Map templateParams) throws Exception {

String result = "";
if (sourceFile != null) {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(sourceFile);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement();
String xmlContent = org.docx4j.XmlUtils.marshaltoString(wmlDocumentEl, true);
result = xmlContent;
}

// Initialise enigine injecting basic properties
VelocityEngine ve = new VelocityEngine();
Properties properties = new Properties();
properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
ve.init(properties);

templateParams.put("DateTool", new ComparisonDateTool());
templateParams.put("MathTool", new MathTool());
templateParams.put("NumberTool", new NumberTool());
templateParams.put("EscapeTool", new EscapeTool());
templateParams.put("DisplayTool", new DisplayTool());
templateParams.put("ConversionTool", new ConversionTool());
templateParams.put("LoopTool", new LoopTool()); 

// Template mergering
VelocityContext context = new VelocityContext(templateParams);
StringWriter writer = new StringWriter();
Velocity.init();
Velocity.evaluate( context, writer, "log tag name", result);

return writer;
}

/**
* Method to do the template merge directly in Ms Word and export out to specifc location
* The format of replaced content is "$colour";
*
* @param sourceFile the source file of .docx
* @param templateParams
*        the contents need to be merged  like:{( "colour", "Red" )}
* @param outputPath the location which the file is to be exported to.
*/

@SuppressWarnings("rawtypes")
public static void exportMergedXMLtoDocx(File sourceFile ,String outputPath, Map templateParams) throws Exception {

if (sourceFile != null) {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(sourceFile);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
StringWriter writer = getMsWordDocumentXMLString(sourceFile, templateParams);

Object obj = XmlUtils.unmarshalString(writer.toString());
documentPart.setJaxbElement((Document) obj);
wordMLPackage.addTargetPart(documentPart);
wordMLPackage.save(new java.io.File(outputPath));
writer.flush();
writer.close();
}
}
}


转载于:https://my.oschina.net/Business/blog/195507

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值