freemark導出word,對日期圖片還未做處理
1.例如:一個word文檔,把基本格式寫好,然後另存爲:.xml文件
2.將.word-template.xml文件放到template|(你存放的文件夾,自定義)文件夾下
3.將word-template.xml文件進行格式化,然後寫一個list標籤,按照word文檔中的定義寫,如圖:
<#list clothingList as clothing></#list>
4.導入word的工具類,同樣適用於Excel,此處的只是簡單針對於字符串的,日期圖片都沒有做處理
/**
*
* 導出word,Excel公用 對於日期圖片不可以
* @param response
* @param map 查詢的list集合,其他一些數據都放到map中
* @param fileName 導入的模板的文件名
* @param suffixName 生成的文件的後綴名 例如:.xlsx .docx
*/
public static void downloadExcel(HttpServletResponse response, Map map,String fileName,String suffixName) {
Configuration configuration = new Configuration();
//configuration.setDefaultEncoding("utf-8");
//有兩種方式獲取你的模板,模板在項目中時用第一個,模板在本地時用第二個。
//注意:兩種方式的路徑都只需要寫到模板的上一級目錄
//configuration.setClassForTemplateLoading(this.getClass(), "/template");
//this.getClass()在static中不可使用,可以使用類名.class獲取class
//template爲模板的文件夾名,需要寫成常量,容易更改
configuration.setClassForTemplateLoading(ImportExcelUtil.class, Const.IMPORT_TEMPLATE_PATH);
// configuration.setDirectoryForTemplateLoading(new File("C:/"));
//File outFile = new File("D:/outFile/"+Math.random()*10000+".xls");//輸出路徑
response.setContentType("application/octet-stream");
//response.setHeader("Content-disposition", "attachment;filename=" + UUID.randomUUID().toString() + ".xlsx");
//後綴名作爲動態的
response.setHeader("Content-disposition", "attachment;filename=" + UUID.randomUUID().toString() + suffixName);
Template t = null;
OutputStream out = null;
OutputStreamWriter writer = null;
try {
// t = configuration.getTemplate("excel-template.xml", "utf-8"); //文件名,獲取模板
//fileName,傳過來的作爲常量
t = configuration.getTemplate(fileName, "utf-8"); //文件名,獲取模板
out = response.getOutputStream();
writer = new OutputStreamWriter(out, "utf-8");
t.process(map, writer);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
常量類:將模板都進行常量存儲:
5.contoller層
/**
* 使用工具類將數據導出word,對於圖片,日期不可以
*
* @param response
*/
@RequestMapping("downloadWord")
public void downloadWord(HttpServletResponse response) {
//查詢出來的數據存放到map中
List list = clothingService.queryAllListNoPage();
Map map = new HashMap();
map.put("clothingList", list);
map.put("time", "2020-11-11");
map.put("phoneClothing", "12235467");
ImportExcelUtil.downloadExcel(response, map, Const.IMPORT_WORD_PATH, ".docx");
}
然後導出就完成了