Excel导出将文件返回给前端

    @RequestMapping("getExcel")
    public void getExcel(InsExcelParas insExcelParas, HttpServletResponse response) throws IOException {
        try {
            if (null!= insExcelParas){
                if (insExcelParas.getImei()!=null && !insExcelParas.getImei().equals("")) {
                    HSSFWorkbook wb = excelUtil.getHSSFWorkbook(new HSSFWorkbook(), insExcelParas);
                    response.reset();
                    response.setContentType("application/octet-stream");
                    response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(insExcelParas.getImei() + new Date().getTime() + ".xlsx", "UTF-8"))));
                    OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                    wb.write(toClient);
                    toClient.flush();
                    toClient.close();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

insExcelParas.getImei() + new Date().getTime() + “.xlsx” 为文件名
wb为生成的Excel

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以使用Apache POI库来导出Excel文件并将其返回前端进行下载。 首先,我们需要在pom.xml文件中添加Apache POI的依赖项: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 接下来,创建一个Excel导出的处理器方法。可以在Controller中创建一个方法,该方法使用`@RequestMapping`或`@GetMapping`注解来指定URL路径和HTTP请求方法。在该方法中,可以使用Apache POI来创建和填充Excel工作簿。例如,以下代码演示了如何在Excel中创建并填充一个简单的表格: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @RequestMapping("/export") public ResponseEntity<Resource> exportExcel() throws IOException { // 创建Excel工作簿 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 填充数据 Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("姓名"); headerRow.createCell(1).setCellValue("年龄"); Row dataRow = sheet.createRow(1); dataRow.createCell(0).setCellValue("张三"); dataRow.createCell(1).setCellValue(25); // 将工作簿转换为字节数组 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); byte[] excelBytes = outputStream.toByteArray(); // 创建HTTP响应体,并设置文件下载的相关头信息 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", "data.xlsx"); // 返回带有Excel文件的响应实体 return new ResponseEntity<>(new ByteArrayResource(excelBytes), headers, HttpStatus.OK); } ``` 在上面的代码中,我们创建了一个简单的Excel表格,并将其转换为字节数组形式。然后,我们创建了一个包含Excel文件的响应实体,并设置了HTTP响应头以指定文件下载的相关信息,例如文件名和Content-Type。最后,我们将响应实体返回前端进行下载。 以上是一个简单的示例,你可以根据实际需求来创建更复杂的Excel导出

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值