package service;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import servlet.FreeMarkServlet;
import sun.misc.BASE64Encoder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
- 使用freemark生成word
- @author stormwy */
public class Freemark {
public static void main(String[] args){
Freemark freemark = new Freemark("/template");
// freemark.setTemplateName("wordTemplate.ftl");
freemark.setTemplateName("word1.ftl");
freemark.setFileName("word6"+".docx");
freemark.setFilePath("WebContent/resources/");
freemark.createWord();
}
public void createWord(){
Template t = null;
try {
t = configuration.getTemplate(templateName);
} catch (IOException e) {
e.printStackTrace();
}
File outFile = new File(filePath+fileName);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// FreeMarkServlet pro = new FreeMarkServlet();
Map<String, Object> map = new HashMap<String, Object>();
map.put("date1", bianxieren);
map.put("date2", shenpiren);
map.put("date3", bianxie);
map.put("date4", shenpi);
/* map.put("image", getImageStr());
map.put("SEX", "男");
map.put("BIRTH", "1987-08");
map.put("ZZMM", "党员");
*/
try {
t.process(map, out);
out.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* freemark初始化
* @param templatePath 模板文件位置
*/
public Freemark(String templatePath) {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
configuration.setClassForTemplateLoading(this.getClass(),templatePath);
}
//插入图片
/*private String getImageStr() {
String imgFile = "D:/hanmanyi/pic/111.jpg";
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}*/
/**
* freemark模板配置
*/
private Configuration configuration;
/**
* freemark模板的名字
*/
private String templateName;
/**
* 生成文件名
*/
private String fileName;
/**
* 生成文件路径
*/
private String filePath;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getTemplateName() {
return templateName;
}
public void setTemplateName(String templateName) {
this.templateName = templateName;
}
}这块是要修改的参数
那个freeMark是一个自己可以运行的的文件,他的功能是可以修改一部分参数,生成一个word文档,但是修改参数的时候必须在文件里修改,所以我就想能不能通过页面输入值给它修改
我自己弄个了简单的前台,我想用前台把值传给这个freeMark,请教了别人,说需要通过servlet,我吧值能传给servlet,但是死活不会从servlet吧值传给freeMark.java了