public class VelocityExample {
/**
* @param args
*/
public static void main(String[] args) {
new VelocityExample().velocityClassLoadExample();
}
public void velocityClassLoadExample() {
// 创建引擎
VelocityEngine ve=new VelocityEngine();
//设置模板加载路径,这里设置的是class下
ve.setProperty(Velocity.RESOURCE_LOADER, "class");
ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
try {
//进行初始化操作
ve.init();
//加载模板,设定模板编码
Template t=ve.getTemplate("module.vm","utf-8");
//设置初始化数据
VelocityContext context = new VelocityContext();
// context.put("name", Encode.encode2HtmlUnicode("贸一个哥哥"));
context.put("checkbox1", "<object classid=3D'CLSID:8BD21D40-EC42-11CE-9E0D-00AA006002F3' id=3DCheckBox2"
+"width=3D144 height=3D24>"
+"<param name=3DVariousPropertyBits value=3D746588191>"
+"<param name=3DBackColor value=3D16777215>"
+"<param name=3DForeColor value=3D0>"
+"<param name=3DDisplayStyle value=3D4>"
+"<param name=3DSize value=3D'3810;635'>"
+"<param name=3DValue value=3D0>"
+"<param name=3DCaption value=3D信息服务>"
+"<param name=3DFontName value=3D宋体>"
+"<param name=3DFontHeight value=3D210>"
+"<param name=3DFontCharSet value=3D134>"
+"<param name=3DFontPitchAndFamily value=3D34>"
+"</object> ");
//设置输出
StringWriter writer = new StringWriter();
//替换模板内容
t.merge(context, writer);
//创建目录路径
File dir=new File("d:");
if(!dir.exists()){
dir.mkdirs();
}
//创建文件
File file = new File(dir.getPath(),"velocity.doc");
if(!file.exists()){
file.createNewFile();
}
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
System.out.println();
out.println(writer.toString());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
velocity导出word模板
猜你喜欢
转载自mengjichen.iteye.com/blog/1431712
今日推荐
周排行