您的位置:首页 > Web前端 > HTML

Velocity模板引擎实战:动态生成HTML、Word、Excel等报表

2018-03-28 14:34 1966 查看
上篇文章介绍了Velocity的基本用法,文章链接:Java使用 VelocityEngine模板引擎快速生成HTML等各种代码

本篇文章主要针对Velocity的实际应用

先来一个工具类

package utils;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

/**
* 用于解析vm模版(world文档保存为xml格式,然后把里面的值改为velocity变量)
* 注意:解析excel的xml模版时
* 必须把<NumberFormat ss:Format="_ * #,##0_ ;_ * \-#,##0_ ;_ * "-"_ ;_ @_ "/> 删掉
* 否则velocity处理模版后,导出的xls会出现内容空白问题
* @author: <a href="mailto:wb-lzl282164@alibaba-inc.com">李智龙</a>
* @date: 2018/3/28
*/
public class VelocityUtil {

private static VelocityEngine ve;
static {
ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
}
public static VelocityEngine getVe() {
return ve;
}
}


再来一个通用测试类

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import utils.VelocityUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author: <a href="mailto:wb-lzl282164@alibaba-inc.com">李智龙</a>
* @date: 2018/3/28
*/
public class HelloVelocity {
public static void main(String[] args) throws IOException {
VelocityEngine ve = VelocityUtil.getVe();
// 载入(获取)模板对象
Template t = ve.getTemplate("wordTemp01.xml");
VelocityContext ctx = new VelocityContext();
// 域对象加入参数值
ctx.put("name", "李智龙");
ctx.put("age", "");
ctx.put("date", (new Date()).toString());
// list集合
List temp = new ArrayList();
temp.add("1");
temp.add("2");
ctx.put("list", temp);
File file = new File("export.doc");
StringWriter sw = new StringWriter();
t.merge(ctx, sw);
InputStream inputStream = new ByteArrayInputStream(sw.toString().getBytes("UTF-8"));
inputstreamtofile(inputStream, file);
System.out.println(sw.toString());
}
public static void inputstreamtofile(InputStream ins, File file){
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


一、HTML导出

参考上篇文章,就是和文本模板一样的道理

Java使用 VelocityEngine模板引擎快速生成HTML等各种代码

二、Word导出

1、新建一个Word(wordTemp.doc),注意圈红的部分



2、另存为xml文件,使用浏览器打开,注意上图标红的两串代码位置不对



3、修改xml后的内容,对篮框内的整体进行循环



4、改完后再次打开模板会发现,红框中的内容隐藏了



5、替换通用测试类中的模板,执行代码

6、效果



三、Excel导出

和world一样保存为xml格式,唯一的区别是,Excel需要把

<NumberFormat ss:Format="_ * #,##0_ ;_ * \-#,##0_ ;_ * "-"_ ;_ @_ "/>


这行干掉(不止一行),否则导出来的是空白
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  报表 java 模板