springboot中使用poi导出excel文件,下载简易模板(根据对象实体类生成表头)

pom依赖

	 <!--导入导出excel-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.0</version>
        </dependency>

完成代码

	@ApiOperation("下载模板")
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response){

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("标题名", "sheet名"), Check.class, new ArrayList<Check>());
        try {
            response.setContentType("application/vnd.ms-excel");
            String fileName = URLEncoder.encode("文件名.xlsx", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
            // 服务端要在header设置Access-Control-Expose-Headers, 前端才能正常获取到
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            OutputStream output = response.getOutputStream();
            workbook.write(output);
            output.flush();
            output.close();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Check实体类中使用 @Excel 注解标注即可

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用 Apache POI实现将 MySQL 查询结果导出Excel 文件。下面是一个简单的示例代码: 1. 添加 Maven 依赖 ```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> ``` 2. 创建 Excel 文件并写入数据 ```java // 创建工作簿 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 执行查询并获取结果集 List<Object[]> resultList = jdbcTemplate.query(sql, args, new BeanPropertyRowMapper<>(clazz)); // 写入表头 Row headerRow = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(headers[i]); } // 写入数据 int rowIndex = 1; for (Object[] row : resultList) { Row dataRow = sheet.createRow(rowIndex++); for (int i = 0; i < row.length; i++) { Cell cell = dataRow.createCell(i); cell.setCellValue(row[i].toString()); } } // 输出 Excel 文件 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); ``` 其,`jdbcTemplate` 是 Spring 提供的 JDBC 操作工具类,`sql` 是要执行的 SQL 语句,`args` 是 SQL 语句的参数,`clazz` 是查询结果对应的实体类,`headers` 是表头数组,`fileName` 是导出Excel 文件名,`response` 是 HttpServletResponse 对象。 以上代码将查询结果写入 Excel 文件并直接输出到浏览器。你可以根据实际需求自行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想养一只!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值