public void exportExcel2(){
TemplateExportParams params = new TemplateExportParams(
“WEB-INF/excel/stu.xlsx”);
Map<String, Object> map = new HashMap<String, Object>();
List slist = studentDao.getAll();
map.put(“num”,2);
List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
Map<String, String> lm;
int i = 1;
for (Student elem:slist){
lm = new HashMap<String, String>();
lm.put("age",elem.getAge());
lm.put("name",elem.getName());
lm.put("sex",elem.getSex());
listMap.add(lm);
i++;
}
map.put("model", listMap);
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
HttpServletResponse response = null;//创建一个HttpServletResponse对象
OutputStream out = null;
response = ServletActionContext.getResponse();//初始化HttpServletResponse对象
response.setHeader("Content-disposition","attachment; filename="+"student"+".xls");//filename是下载的xls的名,建议最好用英文
try {
out=response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String export(){
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType(“text/html;charset=UTF-8”);
studentService.exportExcel2();
System.out.println(“导出”);
return NONE;
}
excel样式