关于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文件
然后就可以了
执行就可以了
目前对于日期还有空的数据导入还存在问题,后续会补上