java鬼混笔记:用Spring的ResponseEntity和poi进行excel生成和下载

来源:互联网 发布:爸爸妈妈的网络用语 编辑:程序博客网 时间:2024/06/13 23:47

笔记来自于愤怒人需求,,,,,,

把代码简化了一下

@RequestMapping("/a")@ResponseBodypublic ResponseEntity<byte[]> a() throws Exception {Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("1");// 分页Sheet sheet2 = wb.createSheet("2");// 分页2Row row = sheet.createRow(0);// 第0+1行Cell cell = row.createCell(0);// 第row行第0+1列cell.setCellValue("abc");// 把所有的数据放在excel中HttpHeaders headers = new HttpHeaders();headers.setContentDispositionFormData("attachment", "1workbook.xls");// new String("线上消费记录".getBytes("GBK"),"iso-8859-1")headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();wb.write(outByteStream);return new ResponseEntity<byte[]>(outByteStream.toByteArray(), headers, HttpStatus.OK);}

ok

原创粉丝点击