SpringBoot中使用POI导出Excel时怎样循环数据库数据赋值

场景

SpringBoot中使用POI实现自定义Excel布局式导出:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89497792

POI实现Excel导出时常用方法说明:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89499051

在已经实现简单的Excel导出的前提下,要结合具体业务实现查询数据库,并

遍历赋值到excel上。

实现

使用MyBatisPlus从数据库查询数据

Map<String,Object> param=new HashMap<String,Object>();
            param.put("in_order_id",id);
            WmsInOrderVO inOrders=this.inOrderMapper.getInOrder(id);
            if(inOrders.getGmtCreat()!=null){
                inOrders.setCreatTimeStr(DateConvert.formatDateToString(inOrders.getGmtCreat(),DateStyle.YYYY_MM_DD));
            }
            if(inOrders.getFinishTime()!=null){
                inOrders.setFinishTimeStr(DateConvert.formatDateToString(inOrders.getFinishTime(),DateStyle.YYYY_MM_DD));
            }
            List<WmsInOrderList> inOrderLists=this.inOrderListMapper.selectByMap(param);
            List<Long> inOrderlistIds=new ArrayList<>();
            for(WmsInOrderList l:inOrderLists){
                inOrderlistIds.add(l.getId());
            }
            List<WmsInOrderDetailsVO> inOrderDetails=new ArrayList<>();
            if(inOrderlistIds.size()>0){
                inOrderDetails=this.inOrderDetailsMapper.getInOrderDetailsByIds(inOrderlistIds);
            }

 遍历生成Excel数据

 x++;
            tempSheet.createRow(x).createCell(y+2).setCellValue("入库单明细");
            tempSheet.getRow(x).createCell(y+3).setCellValue("");
            tempSheet.getRow(x).createCell(y+4).setCellValue("");
            tempSheet.getRow(x).createCell(y+5).setCellValue("");
            tempSheet.getRow(x).createCell(y+6).setCellValue("");
            tempSheet.getRow(x).createCell(y+7).setCellValue("");
            tempSheet.getRow(x).createCell(y+8).setCellValue("");
            tempSheet.getRow(x).createCell(y+9).setCellValue("");
            tempSheet.getRow(x).getCell(y+2).setCellStyle(alignStyle5);
            tempSheet.getRow(x).getCell(y+3).setCellStyle(alignStyle5);
            tempSheet.getRow(x).getCell(y+4).setCellStyle(alignStyle5);
            tempSheet.getRow(x).getCell(y+5).setCellStyle(alignStyle5);
            tempSheet.getRow(x).getCell(y+6).setCellStyle(alignStyle5);
            tempSheet.getRow(x).getCell(y+7).setCellStyle(alignStyle5);
            tempSheet.getRow(x).getCell(y+8).setCellStyle(alignStyle5);
            tempSheet.getRow(x).getCell(y+9).setCellStyle(alignStyle5);
            CellRangeAddress region1 = new CellRangeAddress(x,x,y+2,y+9);
            tempSheet.addMergedRegion(region1);
            x++;
            tempSheet.createRow(x).createCell(y).setCellValue("序号");
            tempSheet.getRow(x).createCell(y+1).setCellValue("物料编号");
            tempSheet.getRow(x).createCell(y+2).setCellValue("物料名称");
            tempSheet.getRow(x).createCell(y+3).setCellValue("数量");
            tempSheet.getRow(x).createCell(y+4).setCellValue("单位");
            tempSheet.getRow(x).createCell(y+5).setCellValue("仓储单元编号");
            tempSheet.getRow(x).createCell(y+6).setCellValue("托盘码");
            tempSheet.getRow(x).createCell(y+7).setCellValue("批次");
            tempSheet.getRow(x).getCell(y).setCellStyle(alignStyle3);
            tempSheet.getRow(x).getCell(y+1).setCellStyle(alignStyle3);
            tempSheet.getRow(x).getCell(y+2).setCellStyle(alignStyle3);
            tempSheet.getRow(x).getCell(y+3).setCellStyle(alignStyle3);
            tempSheet.getRow(x).getCell(y+4).setCellStyle(alignStyle3);
            tempSheet.getRow(x).getCell(y+5).setCellStyle(alignStyle3);
            tempSheet.getRow(x).getCell(y+6).setCellStyle(alignStyle3);
            tempSheet.getRow(x).getCell(y+7).setCellStyle(alignStyle3);
            x++;
            for(WmsInOrderDetailsVO v:inOrderDetails){
                tempSheet.createRow(x).createCell(y).setCellValue(v.getId());
                tempSheet.getRow(x).createCell(y+1).setCellValue(v.getMaterielNumber());
                tempSheet.getRow(x).createCell(y+2).setCellValue(v.getMaterielName());
                tempSheet.getRow(x).createCell(y+3).setCellValue(v.getNum());
                tempSheet.getRow(x).createCell(y+4).setCellValue(v.getUntiy());
                tempSheet.getRow(x).createCell(y+5).setCellValue("");
                tempSheet.getRow(x).createCell(y+6).setCellValue(v.getTrayNumber());
                tempSheet.getRow(x).createCell(y+7).setCellValue(v.getBatch());
                tempSheet.getRow(x).getCell(y).setCellStyle(alignStyle);
                tempSheet.getRow(x).getCell(y+1).setCellStyle(alignStyle);
                tempSheet.getRow(x).getCell(y+2).setCellStyle(alignStyle);
                tempSheet.getRow(x).getCell(y+3).setCellStyle(alignStyle);
                tempSheet.getRow(x).getCell(y+4).setCellStyle(alignStyle);
                tempSheet.getRow(x).getCell(y+5).setCellStyle(alignStyle);
                tempSheet.getRow(x).getCell(y+6).setCellStyle(alignStyle);
                tempSheet.getRow(x).getCell(y+7).setCellStyle(alignStyle);
                x++;
            }

效果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

霸道流氓气质

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

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

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

打赏作者

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

抵扣说明:

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

余额充值