1. 程式人生 > >freemark簡單工具類

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);
}

}