导出代码如下:
public void freeMarkerDownTest(HttpServletResponse response, HttpServletRequest request)
throws UnsupportedEncodingException {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = null;
for (int i = 1; i < 10; i++) {
map = new HashMap<String, Object>();
map.put("dx", i);
map.put("class", i + "班");
map.put("d1", i + "1%");
map.put("d2", i + "2%");
map.put("d3", i + "3%");
map.put("d4", i + "4%");
map.put("d5", i + "5%");
map.put("d6", i + "6%");
map.put("d7", i + "7%");
map.put("d8", i + "8%");
list.add(map);
}
Map<String, Object> testMap = new HashMap<String, Object>();
testMap.put("datas", list);
Configuration configuration = new Configuration();
FreeMarkerUtil handler = new FreeMarkerUtil(configuration);
response.setContentType("application/octet-stream;charset=utf-8");
String fileName = FileControl.encodeFileName(request, "2017-10-23 -学校考勤报表.xls");
response.setHeader("Content-Disposition", "attachment;" + fileName);
try {
String templatePath = "E:/zlsu/Document/marking/framktodoc/";
handler.write(templatePath, "freemark测试1.xml", testMap, response.getWriter());
System.out.println("导出成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
导出完用WPS 打开正常 用EXCEL2016 打开出现文件错误如下:
解决方法:
用OpenOffice.org 3.4.1 打开EXCEL 文件 然后把它 用Oppenoffice 另存为XML, 然后插入对应的 freemarker 指令 把这个 xml文件作为 导出的模板, 导出的excel 就能用
EXCEL2016 打开了如下图: