- pom.xml
- excel
- word
excelorg.freemarker freemarker2.3.30 org.apache.poi poi3.14
https://blog.csdn.net/weixin_44824381/article/details/103409787
File file = new File("XXX\te.xls");
OutputStream output = new FileOutputStream(file);
workbook.write(output);
workbook.close();
word
// @SuppressWarnings("unchecked")
public static void createWord(Map dataMap, String templateName, String filePath,
String fileName) {
try {
//创建配置实例
Configuration configuration = new Configuration();
//设置编码
configuration.setDefaultEncoding("UTF-8");
//获取根目录
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if (!path.exists()) {
path = new File("");
}
System.out.println("path:" + path.getAbsolutePath());
//若是上传目录为/templates/,则能够以下获取:
File upload = new File(path.getAbsolutePath(), "templates/");
if (!upload.exists()) {
upload.mkdirs();
}
System.out.println("upload22222222 url:" + upload.getAbsolutePath());
//指定路径的第一种方式,
//configuration.setClassForTemplateLoading();
//指定路径的第二种方式,具体路径
upload = new File("XXXXXXX\");
System.out.println("upload22222222 url:" + upload.getAbsolutePath());
configuration.setDirectoryForTemplateLoading(upload);
// //ftl模板文件
// configuration.setClassForTemplateLoading(WordUtils.class,"/");
//
//获取模板
Template template = configuration.getTemplate(templateName);
//输出文件
File outFile = new File(filePath + File.separator + fileName);
//如果输出目标文件夹不存在,则创建
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
//将模板和数据模型合并生成文件
Writer out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
//生成文件
template.process(dataMap, out);
//关闭流
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2、Main方法
dataMap.put("product", dataMap);
dataMap.put("listInfo", list);
//文件唯一名称
String fileonlyName = "生成Word文档.doc";
WordUtils.createWord(dataMap, "test.ftl", filePath, fileOnlyName);
3、freemarker标签
<#list listInfo as list> #list>
##效果
1、word:
2、excel
1、http://www.javashuo.com/article/p-dinlgnjl-nq.html
2、https://www.cnblogs.com/h-java/p/10026850.html



