1、不用工具类,直接方法导出
@RequestMapping("/exportexcel")
public void exportexcel(HttpServletResponse response) {
OutputStream oStream = null;
try {
// 创建工作簿
HSSFWorkbook wb = new HSSFWorkbook();
// 创建sheet
HSSFSheet sheet = wb.createSheet("列表");
// 创建表头
HSSFRow row = sheet.createRow(0);
//创建单元格
HSSFCell cell = row.createCell(0);
cell.setCellValue("护士id");
HSSFCell cell1 = row.createCell(1);
cell1.setCellValue("项目");
HSSFCell cell2 = row.createCell(2);
cell2.setCellValue("时间");
HSSFCell cell3 = row.createCell(3);
cell3.setCellValue("类别");
HSSFCell cell4 = row.createCell(4);
cell4.setCellValue("成绩");
//创建表头
HSSFRow lrows = sheet.createRow(1);
HSSFCell lcells = lrows.createCell(0);
lcells.setCellValue("导入时请删除本行!!注:需要填写护士id、项目名称、时间、成绩。类别默认为护理部,531就代表是护理部操作,不用改");
HSSFRow lrow = sheet.createRow(2);
//创建单元格
HSSFCell lcell = lrow.createCell(0);
lcell.setCellValue("10000");
HSSFCell lcell1 = lrow.createCell(1);
lcell1.setCellValue("全体护理人员护士条例考试");
HSSFCell lcell2 = lrow.createCell(2);
lcell2.setCellValue("2018-03-08");
HSSFCell lcell3 = lrow.createCell(3);
lcell3.setCellValue("531");
HSSFCell lcell4 = lrow.createCell(4);
lcell4.setCellValue("89");
//根据response获取输出流
response.setContentType("application/force-download"); // 设置下载类型
response.setHeader("Content-Disposition","attachment;filename=sxlb.xls"); // 设置文件的名称
oStream = response.getOutputStream(); // 输出流
//把工作薄写入到输出流
wb.write(oStream);
} catch (Exception e) {
// TODO: handle exception
try {
oStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
2、在jsp页面加上方法
<button type="button" onclick="exportexcel()">导出模板</button>
function exportexcel(){
window.location.href="hrm/exportexcel.do"
}