1.循环N次
<!-- 循环5次 -->
<#list 1..5 as i>${i}</#list>
2.默认值
<!--当name为null时设置默认值:使用!+默认值-->
${name!"名"}
3.变量是否存在
<!--存在为true-->
name??
4.list序号
<!--内置index函数,索引从0开始-->
<#list names as name>${name?index + 1}</#list>
5.模板内容写入流
public static void loadExcelStream(String templateName, Map data, OutputStream outputStream) {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
OutputStreamWriter writer = null;
try {
cfg.setClassForTemplateLoading(FreemarkerUtils.class, "/static");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setClassicCompatible(true);
Template temp = cfg.getTemplate(templateName, "utf-8");
writer = new OutputStreamWriter(outputStream, "utf-8");
temp.process(data, writer);
writer.flush();
} catch (IOException | TemplateException e) {
e.printStackTrace();
throw new RuntimeException("load fail file");
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}