--------------------------------html------------------------------------------------
<a href="<%=basePath%>suspect/TemplateSelect.do">下载excel模板</a>
--------------------------------java------------------------------------------------
@RequestMapping(value = "/TemplateDownload", method = RequestMethod.GET)
@ResponseBody
public void TemplateDownload(HttpServletResponse response) throws IOException {
try {
//创建Excel文档
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
// 设置行宽为20
sheet.setDefaultColumnWidth(20);
// 创建样式并设置背景颜色为灰色
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//创建表头
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("身份证号码");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("照片地址");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("关系人数");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("关系人");
cell.setCellStyle(style);
//设置文件名和文件类型
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=ZDRY.xls" );
//写入Excel文档并输出到输出流
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}