需求:
基于poi的xls文件下载
实现:
控制层:
@RequestMapping(value = "/exportxls", method = RequestMethod.GET)
public void exportxls(HttpServletRequest request, QueryItem queryItem,
HttpServletResponse response) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建工作表对象并命名
Sheet sheet = workbook.createSheet();
/* header start */
Row rowHeader = sheet.createRow(0);
Cell cell1 = rowHeader.createCell(0, Cell.CELL_TYPE_STRING);
cell1.setCellValue("ID");
Cell cell2 = rowHeader.createCell(1, Cell.CELL_TYPE_STRING);
cell2.setCellValue("姓名");
Cell cell3 = rowHeader.createCell(2, Cell.CELL_TYPE_STRING);
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="
+ CommonUtils.getUUID() + ".xls");
OutputStream out = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
workbook.write(out);
out.flush();
IOUtils.closeQuietly(out);
}
?
原文:http://snv.iteye.com/blog/2176307