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

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

时间:2023-03-12 11:04:00浏览次数:38  
标签:SpringBoot setCellStyle POI Excel createCell getRow tempSheet getCell setCellVal


场景


在已经实现简单的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++;
}

效果

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

标签:SpringBoot,setCellStyle,POI,Excel,createCell,getRow,tempSheet,getCell,setCellVal
From: https://blog.51cto.com/BADAOLIUMANGQZ/6115617

相关文章

  • IDEA中进行SpringBoot整合spring-ws开发webservice接口后期要修改xsd文件流程
    场景SpringBoot整合spring-ws开发webservice接口(全流程详细教程)现在接口已经开发完毕,但是在后期进行调试时发现在C#程序中进行调用时某个接口没有响应。原因是不同接口中存......
  • Winforn中导入Excel并显示然后获取多选框选中的内容
    场景使用NPOI导入Excel并赋值给DataTable,然后显示在DataGrdView上,并且添加多选框,然后获取选中行的内容。Winform中使用NPOI实现Excel导入并赋值给DataTable:效果 实现在上......
  • [POI2001][HAOI2007] 反素数 题解
    前置知识:一些关于约数的小常识。唯一分解定理对于所有正整数\(n\),一定有唯一分解方式\(n=p_1^{c_1}p_2^{c_2}\cdotsp_m^{c_m}\),其中\(p_1<p_2<\cdots<p_m\),......
  • P3530[POI2012 FES-Festival] 题解
    题面链接简要题意对于数列\(\{v_n\}\),有两种约束\(v_i=v_j+1\)和\(v_i\gev_j\),问\(\{v_n\}\)最多有多少个不同的项。解法考虑先建图,注意到如果约束图是DAG,那么......
  • 使用POI导出数据到Excel
    0.准备工作自行去apache的maven仓库查找以下关于poi的依赖(x.x为版本号)1.poi-x.x.jar2.poi-ooxml-x.x.jar3.xmlbeans-x.x.x.jar4.poi-ooxml-schemas-x.x.jar5.poi-o......
  • springboot打包时遇到的问题
    第一个问题是Failedtoexecutegoalorg.apache.maven.plugins:maven-resources-plugin:3.3.0:resources(default-resources)onproject   我的springboot版本......
  • SpringBoot 文件上传+拦截器
    SpringBoot文件上传+拦截器文件上传原理表单的enctype属性规定在发送到服务器之前应该如何对表单数据进行编码。当表单的enctype="application/x-www-form-urlencoded......
  • easyexcel填坑-校验表头为空,或者不符合预期
    背景:easyexcelv3.1.5实体类已经使用注解@ExcelProperty标注需要导入的属性正文开始关闭忽略空行,防止第一行是空跳过校验 ignoreEmptyRow(false)。此处如果未关闭,第......
  • 如何通过C++ 将数据写入 Excel 工作表
    直观的界面、出色的计算功能和图表工具,使Excel成为最流行的个人计算机数据处理软件。在独立的数据包含的信息量太少,而过多的数据又难以理清头绪时,制作成表格是数据管理的最......
  • Springcloud学习笔记56--SpringBoot之GET请求与Post请求参数偶发性丢失问题
    1.概述最近遇到一个偶现的问题,在向服务端请求的时候,偶尔会出现异常,在请求中的queryString传递了参数,却出现了异常MissingServletRequestParameterException如下所示:or......