關於freemark將數據導入Excel
1.在Excel中生成你需要的格式,樣式,顏色
如圖:然後${實體類的縮寫.屬性名}
2.另存爲.xml文件
3.將生成的excel-template.xml文件放到你的項目中
resource文件夾下建一個template
4.工具類:
@RequestMapping("downloadExcel3")
public void downloadExcel3(HttpServletResponse response) {
//獲取數據
List list = clothingService.queryAllListNoPage();
Map map = new HashMap();
//把數據存放到Map中,並將所有的信息都存到map中例如:autor等夠可以進行顯示
map.put("clothingList", list);
//map.put("productList", list);
Configuration configuration = new Configuration();
//configuration.setDefaultEncoding("utf-8");
//有兩種方式獲取你的模板,模板在項目中時用第一個,模板在本地時用第二個。
//注意:兩種方式的路徑都只需要寫到模板的上一級目錄
configuration.setClassForTemplateLoading(this.getClass(), "/template");
// 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");
Template t = null;
OutputStream out = null;
OutputStreamWriter writer = null;
try {
t = configuration.getTemplate("excel-template.xml", "utf-8"); //文件名,你生成的excel-template.xml模板的名字,必須一樣
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.編輯excel-template.xml文件
然後就可以了
執行就可以了
目前對於日期還有空的數據導入還存在問題,後續會補上