SSM框架下的POI导出Excel文件功能

超级简单好用的poi组件导出Excel功能:

一、pom.xml导入poi的jar包
	<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
      <dependency>
           <groupId>org.apache.poi</groupId>
           <artifactId>poi</artifactId>
           <version>3.14</version>
       </dependency>
二、jsp代码
<a href="/export/exportXls" type="button" class="btn btn-sm btn-default" ><i class="fa fa-upload"></i> 导出</a>
三、Controller层
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {

        Workbook wb = datasService.exportXls();
        // 文件下载的响应头(让浏览器访问资源的的时候以下载的方式打开,filename文件名)
        response.setHeader("Content-Disposition","attachment;filename=export.xls");
        // 创建 excel 文件
        // 把 excel 的数据输出给浏览器
        wb.write(response.getOutputStream());
    }
四、Service层接口
public interface DatasService {
    Workbook exportXls();
}
五、Service层实现类
    public Workbook exportXls() {
        // 创建 excel 文件
        Workbook wb = new HSSFWorkbook();
        // 创建 sheet
        Sheet sheet = wb.createSheet("学生名单");
        // 标题行
        Row row = sheet.createRow(0);
        // 设置内容到单元格中
        row.createCell(0).setCellValue("学号");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("性别");
        // 查询员工数据
        List<Students> students = employeeMapper.selectAll();
        for (int i = 0; i < students.size(); i++) {
            Students student = students.get(i);
        // 创建行(每个员工就是一行)
            row = sheet.createRow(i + 1);
        // 设置内容到单元格中
            row.createCell(0).setCellValue(students.getStudentsId());
            row.createCell(1).setCellValue(students.getName());
            row.createCell(2).setCellValue(students.getSex());
        }
        return wb;
    }

图示:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

理想展翅飞翔

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

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

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

打赏作者

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

抵扣说明:

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

余额充值