springboot 導出excel word
- pom.xml
-
- excel
- word
pom.xml
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<!--poi-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
excel
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
/**
* 生成word檔案
*
* @param dataMap word中需要展示的動态資料,用map集合來儲存
* @param templateName word模闆名稱,例如:test.ftl
* @param filePath 檔案生成的目标路徑,例如:D:/wordFile/
* @param fileName 生成的檔案名稱,例如:test.doc
*/
// @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";
/** 生成word 資料包裝,模闆名,檔案生成路徑,生成的檔案名*/
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