freeMarker生成word

import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

import sun.misc.BASE64Encoder;

/**
 * 自定義載入模板
 * @author Administrator
 *
 */
public class DocumentGenerator {
	private Configuration configuration;

	/**
	 * word生成器--->構造函數
	 */
	public DocumentGenerator() {
		configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
		configuration.setClassicCompatible(true);
		//讀取模板進行配置
		configuration.setTemplateLoader(new ByteArrayStreamTemplateLoader(new ByteArrayInputStream(getBytesFromFile(new File("F:/word/employ.ftl")))));
	}

	/**
	* @param fileName  模板文件
	* @param outFileName 目標文件
	* @param dataMap  數據集
	*/
	public void createDoc(String fileName, String outFileName, Map dataMap) {
		Template t = null;
		try {
			t = configuration.getTemplate(fileName);  //獲取模板文件
		} catch (IOException e) {
			e.printStackTrace();
		}
		File outFile = new File(outFileName);  //建立目標文件
		Writer out = null;  //建立輸出流
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		try {
			t.process(dataMap, out);   //生成了word文檔
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 獲取要讀取的文件字節碼
	 * @param 要讀取的文件
	 */
	public byte[] getBytesFromFile(File f) {
		if (f == null) {
			return null;
		}
		try {
			FileInputStream stream = new FileInputStream(f);
			ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = stream.read(b)) != -1){
				out.write(b, 0, n);
			}
			stream.close();
			out.close();
			return out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getImageStr(String imgPath) {
		InputStream in = null;
		byte[] data = null;
		try {
			in = new FileInputStream(imgPath);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);
	}
}

/**
 * 載入字節碼模板
 */
class ByteArrayStreamTemplateLoader implements TemplateLoader {
	InputStream in = null;

	/**
	 * 構造函數
	 * @param inputStream
	 */
	public ByteArrayStreamTemplateLoader(ByteArrayInputStream inputStream) {
		in = inputStream;
	}

	/**
	 * 尋找源模板
	 */
	public Object findTemplateSource(String name) throws IOException {
		System.out.println("findTemplateSource");
		return in;
	}
	
	/**
	 * 獲取讀入的模板流文件
	 */
	public Reader getReader(Object templateSource, String encoding) throws IOException {
		System.out.println("getReader");
		return new InputStreamReader(in);
	}

	/**
	 * 關閉模板流文件
	 */
	public void closeTemplateSource(Object templateSource) throws IOException {
		System.out.println("closeTemplateSource");
		if (in != null) {
			in.close();
		}
	}

	public long getLastModified(Object arg0) {
		return 0;
	}
}
相關文章
相關標籤/搜索