实现下载poi导出excel的代码
时间: 2023-12-10 09:22:47 浏览: 29
好的,以下是实现下载poi导出excel的代码示例:
// 导出excel
@RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
public void downloadExcel(HttpServletResponse response) throws Exception {
// 创建excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
// 创建表头
HSSFRow header = sheet.createRow(0);
header.createCell(0).setCellValue("姓名");
header.createCell(1).setCellValue("年龄");
header.createCell(2).setCellValue("性别");
// 填充数据
HSSFRow row1 = sheet.createRow(1);
row1.createCell(0).setCellValue("张三");
row1.createCell(1).setCellValue(20);
row1.createCell(2).setCellValue("男");
HSSFRow row2 = sheet.createRow(2);
row2.createCell(0).setCellValue("李四");
row2.createCell(1).setCellValue(25);
row2.createCell(2).setCellValue("女");
// 设置响应头
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=example.xls");
// 写入excel文件
workbook.write(response.getOutputStream());
workbook.close();
}