freemark简单工具类

package com.broadtext.tdc.report.mftable.others;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import javax.servlet.ServletContext;
import freemarker.cache.MruCacheStorage;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
*
* Freemark 导出excel 的简单工具
* 其他的可解析文件同样适用.
* 同样 日期问题 没有解决;(最好用java.sql包下的日期)
* @Time 下午03:44:36
* @author wsliang
*/
public class FreeMarkUtils {

// 初始化configuration
private static Configuration cfg = null;
static{
//创建配置实例
cfg = new Configuration();

//指定模板如何查看数据模型
cfg.setObjectWrapper(new DefaultObjectWrapper());

//设置共享变量,或者共享方法
// cfg.setSharedVariable("upper", new TemplateModel());

//设置缓存 字符编码 等.
cfg.setCacheStorage(new MruCacheStorage(20, 260));
cfg.setDefaultEncoding("UTF-8");
//cfg.setTimeFormat("yyyy-MM-dd");
}
/**
*
* 通过传入 ServletContext 方法得到 template实例
* @param context ServletContext
* @param filePath 相对web服务器的相对地址目录
* @param filename
* @return template
* @throws IOException
*/
public static Template getTemplate(ServletContext context,String filePath,String fileName) throws IOException{

//设置模板的根目录,加载模板的一种设置共3中
cfg.setServletContextForTemplateLoading(context, filePath);

//从Configuration实例中获取模板实例,这里存储的都是解析过的模板内容
Template temp = cfg.getTemplate(fileName,"UTF-8");
//修改配置参数
//temp.setLocale(Locale.CHINA);
return temp;
}

/**
*
* 通过传入文件目录和文件名 方法得到 template实例
* @param filePath 绝对路径或者相对工程加载路径
* @param filename
* @return Template
* @throws IOException
*/
public static Template getTemplate(String filePath,String fileName) throws IOException{

//设置模板的根目录,加载模板的一种设置共3中
cfg.setDirectoryForTemplateLoading(new File(filePath));

//从Configuration实例中获取模板实例,这里存储的都是解析过的模板内容
Template temp = cfg.getTemplate(fileName,"UTF-8");

//修改配置参数
//temp.setLocale(Locale.CHINA);
return temp;
}

/**
*
* 通过类加载 方法得到 template实例
* @param clases
* @param filePath
* @param filename
* @return
* @throws IOException
*/
public static Template getTemplate(Class clases,String filePath,String fileName) throws IOException{

//设置模板的根目录,加载模板的一种设置共3中
cfg.setClassForTemplateLoading(clases,filePath);

//从Configuration实例中获取模板实例,这里存储的都是解析过的模板内容
Template temp = cfg.getTemplate(fileName,"UTF-8");

//修改配置参数
//temp.setLocale(Locale.CHINA);
return temp;
}

/**
*
* 方法说明
* @param filePath 相对 context 的文件地址
* @param fileName
* @param data 数据对象
* @return InputStream
* @throws IOException
* @throws TemplateException
*/
public static InputStream getInputStream(String filePath,String fileName,Object data,ServletContext context) throws TemplateException, IOException{

ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStreamWriter out = new OutputStreamWriter(baout,"UTF-8");
getTemplate(context, filePath, fileName).process(data, out);
out.flush();
return new ByteArrayInputStream(baout.toByteArray());
}
/**
*
* 方法说明
* @param filePath 相对 clases 的文件地址
* @param fileName
* @param data
* @param clases
* @return InputStream
* @throws TemplateException
* @throws IOException
*/
public static InputStream getInputStream(String filePath,String fileName,Object data,Class clases) throws TemplateException, IOException{

ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStreamWriter out = new OutputStreamWriter(baout,"UTF-8");
getTemplate(clases, filePath, fileName).process(data, out);
out.flush();
return new ByteArrayInputStream(baout.toByteArray());
}
/**
*
* 方法说明
* @param filePath 文件绝对地址或者相对工程的地址目录
* @param fileName
* @param data
* @return InputStream
* @throws TemplateException
* @throws IOException
*/
public static InputStream getInputStream(String filePath,String fileName,Object data) throws TemplateException, IOException{

ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStreamWriter out = new OutputStreamWriter(baout,"UTF-8");
getTemplate(filePath, fileName).process(data, out);
out.flush();
return new ByteArrayInputStream(baout.toByteArray());
}

public static void mixTempletAndData(String filePath,String fileName,Object data,Writer out) throws TemplateException, IOException{
getTemplate(filePath, fileName).process(data, out);
out.flush();
}

public static void main(String[] args) throws TemplateException, IOException {
// test.xml 的文件地址为 com/wsliang/test/test.xml
FreeMarkUtils.getInputStream("com/wsliang/test", "/test.xml", new HashMap(), FreeMarkUtils.class);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值