Jeesite(SpringBoot)采用poi实现从数据库提取数据导出为Excel,巨详细!!

本文介绍如何在SpringBoot项目中使用Apache POI库进行Excel文件的导出。通过具体代码示例,展示了从数据库查询数据并将其转换为Excel格式的过程,包括样式设置、单元格合并等细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 技术说明:

主要是SpringBoot一套

思路:写个XML查询数据 查询到后通过get属性方法放到Excel里

  1. Maven POM:

<!--POI对office操作jar包-->
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>3.14</version>
</dependency>

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>3.14</version>
</dependency>
  1.  实体类和service+Dao就不贴了很简单 就是一个查询出你要的数据而已

Controller

@RequestMapping(value = "export")
public void ExportTemplents(HttpServletResponse response, UserInfo userInfo) throws IOException {
    //查询数据
    List<UserInfo> userInfoList = userInfoService.QueryExpert(userInfo);
    HSSFWorkbook wb = new HSSFWorkbook();//创建工作簿 HSSFWorkbook:Excel的文档对象
    HSSFFont font = wb.createFont();//设置字体大小 为什么要用wb.createFont呢 因为wb是主 font是包含这个里面的
    HSSFCellStyle style = wb.createCellStyle(); //设置单元格格式
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中对齐格式
    font.setFontHeightInPoints((short) 28);//设置字体
    HSSFSheet sheet = wb.createSheet("专家信息表");//
    HSSFRow row = null; //创建行
    row = sheet.createRow(0);//创建第一行单元格.就像数组一样是0开头的
    row.setHeight((short) (26.5 * 20));//设置行高

    row.createCell(0).setCellValue("专家用户信息表"); //设置第一行单元格设置值
    HSSFPatriarch patr = sheet.createDrawingPatriarch();//HSSFPatriarch poi的划线方法
    HSSFComment comment = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
    // 设置注释内容
    comment.setString(new HSSFRichTextString("专家状态 0:正常,1,冻结,2待审核,3审核通过,4审核未通过,5删除!"));

    //设置单元格合并 参数是:起始行号,终止行号, 起始列号,终止列号
    CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 2);
    sheet.addMergedRegion(rowRegion);
      //sheet是页 这个的意思在文档Sheet的第一行
    row = sheet.createRow(1);
    /*    row.setHeight((short) (22.50 * 20));*/
    //设置单元格格式
    row.setHeight((short) (20.29 * 20));
    //第一行标签
    row.createCell(0).setCellValue("专家id");
    row.createCell(1).setCellValue("专家姓名");
    row.createCell(2).setCellValue("专家账号");
    row.createCell(3).setCellValue("专家手机号");
    row.createCell(4).setCellValue("专家所属公司");
    row.createCell(5).setCellValue("专家状态");
    row.createCell(6).setCellValue("专家创建时间");
    row.createCell(7).setCellValue("性别");

    //遍历集合数据,产生数据行
    for (int i = 0; i < userInfoList.size(); i++) {
        row = sheet.createRow(i + 2);
        UserInfo userInfo1 = userInfoList.get(i);
        //往表格添加数据
        row.createCell(0).setCellValue(userInfo1.getId());
        row.createCell(1).setCellValue(userInfo1.getUserName());
        row.createCell(2).setCellValue(userInfo1.getNickname());
        row.createCell(3).setCellValue(userInfo1.getPhone());
        row.createCell(4).setCellValue(userInfo1.getCompany());
        //日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(userInfo1.getCreateDate());
        row.createCell(6).setCellValue(time);

   //性别处理 数据库存放的是1,2 我写的麻烦 你们自己处理吧
        if(StringUtils.isNotBlank(userInfo1.getSex())){
        if ( userInfo1.getSex().equlas("1")) {
           
            row.createCell(7).setCellValue("男");
        }
        else {
            row.createCell(7).setCellValue("女");
        }
    }

    }
    sheet.setDefaultRowHeight((short) (20.29 * 20));
    for (int i = 0; i <= 6; i++) {
        sheet.autoSizeColumn(i);//自动行高
    }
   // response.setContentType 设置格式为UTF-8 不然可能会乱码
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
   //output流得到流
    OutputStream os = response.getOutputStream();
    response.setHeader("Content-dEposition", "attachment;filename=专家信息.xls");
    wb.write(os);
    os.flush();//刷新流
    os.close();//关闭流
}

  • 忘了发前端页面是咋样的。。。。补上
<div style="margin-left: 10px">

   <a href="${ctx}/yinyue/userInfo/export"><button type="button" class="btn btn-primary">导出</button></a>
</div>

a href="${ctx}/yinyue/userInfo/export"这东西是你的请求地址,根据自己项目修改

另外有大神总结了poi的使用详解 我把地址贴这里 去看看就明白很多了

POI使用详解 - 空谷幽澜 - 博客园

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值