导出excel调用的方法
/** * * @param list 导出的数据集合 * @param request * @param response */ public void exportExcel(List<Student> list, HttpServletRequest request, HttpServletResponse response) { HSSFWorkbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet(); boolean flag = false; String headerTitle="编号,用户名"; //表头 String[] headerTitles=headerTitle.split(","); try { Row row = sheet.createRow(0); for (int i = 0; i < headerTitles.length; i++) { Cell cell = row.createCell(i); cell.setCellValue(headerTitles[i]); } for (int i = 0; i < list.size(); i++) { Row rowB = sheet.createRow(i + 1); Cell cellB1 = rowB.createCell(0); cellB1.setCellValue(list.get(i).getId()); Cell cellB2 = rowB.createCell(1); cellB2.setCellValue(list.get(i).getUsername()); } String filename = "文件名" + DateFormatUtils.formatDateTime(new Date()) + ".xls"; if (wb != null) { if (response != null) { response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename="" + new String(filename.getBytes("gb2312"), "ISO8859-1")); //文件名中文需进行格式转换,不然可能出现乱码 OutputStream out = response.getOutputStream(); wb.write(out); out.close(); } } } catch (Exception e) { e.printStackTrace(); } }