|
|
|
|
|
<%@ page import="java.io.*" %>
|
|
|
<%@ page import="weaver.hrm.User" %>
|
|
|
<%@ page import="java.util.*" %>
|
|
|
<%@ page import="freemarker.template.Configuration" %>
|
|
|
<%@ page import="freemarker.template.TemplateExceptionHandler" %>
|
|
|
<%@ page import="freemarker.template.Template" %>
|
|
|
<%@ page import="java.nio.charset.StandardCharsets" %>
|
|
|
|
|
|
|
|
|
<%
|
|
|
String TemplatePath = "/opt/weaver/filesys/";
|
|
|
//String TemplateName = "html模版.ftl";
|
|
|
String TemplateName = "qwer.ftl";
|
|
|
Map<String, Object> root = new HashMap<>();
|
|
|
// root.put("title", "期末成绩表");
|
|
|
|
|
|
ArrayList<Map> list = new ArrayList<>();
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
map.put("lastname",new User(22).getLastname() );
|
|
|
map.put("subject", new User(22).getLastname() );
|
|
|
map.put("achievement", "98");
|
|
|
list.add(map);
|
|
|
Map<String, String> map2 = new HashMap<>();
|
|
|
map2.put("lastname", new User(22).getLastname() );
|
|
|
map2.put("subject", new User(22).getLastname());
|
|
|
map2.put("achievement", "97");
|
|
|
list.add(map2);
|
|
|
list.add(map2);
|
|
|
list.add(map2);
|
|
|
list.add(map2);
|
|
|
list.add(map2);
|
|
|
|
|
|
ArrayList<Map> lists = new ArrayList<Map>();
|
|
|
root.put("achievementList", list);
|
|
|
lists.add(root);
|
|
|
lists.add(root);
|
|
|
lists.add(root);
|
|
|
|
|
|
Map<String, Object> roots = new HashMap<String, Object>();
|
|
|
System.out.println(lists.size());
|
|
|
roots.put("testlist",lists);
|
|
|
//generate(TemplatePath, TemplateName, root, "shengcheng.html");
|
|
|
generate(TemplatePath, TemplateName, roots, "shengcheng1.docx");
|
|
|
|
|
|
%>
|
|
|
|
|
|
<%!
|
|
|
/**
|
|
|
* @param TemplatePath 模版存放目录
|
|
|
* @param TemplateName 模版名
|
|
|
* @param root 填充的数据
|
|
|
* @param GenerateFilename 生成文件名
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static void generate(String TemplatePath, String TemplateName, Map<String, Object> root, String GenerateFilename) throws Exception {
|
|
|
Configuration cfg = new Configuration();
|
|
|
//指定模板文件的来源目录
|
|
|
cfg.setDirectoryForTemplateLoading(new File(TemplatePath));
|
|
|
cfg.setDefaultEncoding("UTF-8");
|
|
|
//设置错误的显示方式(日志)
|
|
|
//在生产系统中:TemplateExceptionHandler.RETHROW_HANDLER 默认值
|
|
|
//在开发HTML模板期间:TemplateExceptionHandler.HTML_DEBUG_HANDLER
|
|
|
//在开发非HTML模板期间:TemplateExceptionHandler.DEBUG_HANDLER
|
|
|
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
|
|
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
|
|
|
|
|
|
|
|
//获取模板文件
|
|
|
Template temp = cfg.getTemplate(TemplateName);
|
|
|
//合并模板和数据模型
|
|
|
File file = new File(TemplatePath + "new-built");
|
|
|
//如果文件夹不存在,则创建文件夹
|
|
|
if (!file.exists()) {
|
|
|
//file.mkdirs();//多级目录
|
|
|
file.mkdir();//只创建一级目录
|
|
|
}
|
|
|
//Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(TemplatePath + "new-built/" + GenerateFilename)));//输出文件
|
|
|
// FileWriter out = new FileWriter(TemplatePath + "new-built/" + GenerateFilename);
|
|
|
//Writer out = new OutputStreamWriter(System.out);//输出控制台
|
|
|
//StringWriter out = new StringWriter();//输出为字符串,可作为接口动态返回
|
|
|
|
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(TemplatePath + "new-built/" + GenerateFilename);
|
|
|
Writer out = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
|
|
|
temp.process(root, out);
|
|
|
out.flush();//可不手动调用
|
|
|
}
|
|
|
|
|
|
public static String codeUtf8(String text) {
|
|
|
String ISOtext = null;
|
|
|
ISOtext = new String(text.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
|
|
|
return new String(ISOtext.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
|
|
|
|
|
|
}
|
|
|
|
|
|
%>
|
|
|
|
|
|
|