Velocity+docx4j 模板替换
2014-01-24 13:25
211 查看
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
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(); } } }
相关文章推荐
- java替换pdf模板出现中文乱码问题
- 模板替换IMG正则
- TP模板替换默认的替换规则
- 使用POI操作Excel修改模板(批量替换excel中的数据)
- dedecms5.6 简单替换网站模板
- 模板:正则替换之后生成标准的php文件 然后include该文件
- asp简单生成静态的方法(模板标签替换)
- IBM Lotus Domino邮件数据库模板替换命令详细解释
- 会声会影分分钟搞定模板替换
- SQL循环替换模板参数的样码
- myeclipse的servlet模板替换
- “字符串替换” 和 “模板设置” (application/config.php)
- thinkphp中模板替换
- 利用NVelocity模板替换技术显示页面,模板里怎么调用C#类库的静态方法
- ecshop小京东模板切换到smarty3.13之模板函数,用模板函数替换楼层
- 小程序框架模板替换...
- ArcGIS Engine 怎样给PageLayout替换模板
- zen cart -- 批量替换掉多个zen cart网站中的某个模板文件或语言文件
- handlebars模板替换