excel 动态列导出

1
excel动态列,只好用poi来写了,也并不复杂,一样就这个件事情抽像为几步,就是套路了,开发效率就上去了。
1 准备空模板
导出操作与excel模板的导出一样,可以参考excel导出标准化
1
2 自定义SheetWriteHandler
要通过pos自己创建每一样,像模板一样创建即可.

WriteSheet sheet0 = EasyExcel.writerSheet(0)//标题.registerWriteHandler(new GoodsInvRdSumWriteHandler(goodsInvRdSumListDto.getHeader())).build();

主要重写afterSheetCreate,也就是一行行的创建excel模板

 @Overridepublic void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {Workbook workbook = writeWorkbookHolder.getWorkbook();this.centerCellStyle = createCellContentStyle(workbook,HorizontalAlignment.CENTER,BorderStyle.THIN);this.leftCellStyle = createCellContentStyle(workbook,HorizontalAlignment.LEFT,BorderStyle.THIN);this.rightCellStyle = createCellContentStyle(workbook,HorizontalAlignment.RIGHT,BorderStyle.THIN);Sheet sheet = workbook.getSheetAt(0);row1(sheet,workbook);row2(sheet,workbook);row34(sheet);row5(sheet);}

第一行

  /*** 第一行是标题* @param sheet*/private void row1(Sheet sheet,Workbook workbook){Row row = sheet.createRow(0);row.setHeight((short) (50 * 20));Cell cell = row.createCell(0);cell.setCellValue("商品收发汇总表");cell.setCellStyle(getHeadCellStyle(workbook, this.centerCellStyle));CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 9+this.dynamicHeader.size()*2-1);sheet.addMergedRegionUnsafe(cellRangeAddress);setMergedRegionStyleNoBorder(sheet, cellRangeAddress);}

第二行

/*** 第二行 公司名称、日期* @param sheet*/private void row2(Sheet sheet,Workbook workbook){Row row = sheet.createRow(1);CellStyle subHeaderStyle = createCellContentStyle(workbook, HorizontalAlignment.LEFT,BorderStyle.NONE);// 公司名称Cell cell = row.createCell(0);cell.setCellStyle(subHeaderStyle);cell.setCellValue("公司:{companyName}                               日期:{startBillDate}至{endBillDate}");sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 1, 0, 9+this.dynamicHeader.size()*2-1));}

第三行,第四行涉及到动态列的创建和合并表头

 private void row34(Sheet sheet){Row row3 = sheet.createRow(2);Row row4 = sheet.createRow(3);// 商品编码Cell cell = row3.createCell(0);cell.setCellValue("商品编码");cell.setCellStyle(this.centerCellStyle);CellRangeAddress cellRangeAddress = new CellRangeAddress(2, 3, 0, 0);sheet.addMergedRegionUnsafe(cellRangeAddress);setMergedRegionStyle(sheet, cellRangeAddress);// 商品名称cell = row3.createCell(1);cell.setCellValue("商品名称");cell.setCellStyle(this.centerCellStyle);cellRangeAddress = new CellRangeAddress(2, 3, 1, 1);sheet.addMergedRegionUnsafe(cellRangeAddress);setMergedRegionStyle(sheet, cellRangeAddress);// 商品规格cell = row3.createCell(2);cell.setCellValue("商品规格");cell.setCellStyle(this.centerCellStyle);cellRangeAddress = new CellRangeAddress(2, 3, 2, 2);sheet.addMergedRegionUnsafe(cellRangeAddress);setMergedRegionStyle(sheet, cellRangeAddress);//动态列int dySize = this.dynamicHeader.size();if (dySize>0){for (int i=0; i<dySize; i++){Map<String,Object> colMap = this.dynamicHeader.get(i);String busiType = String.valueOf(colMap.get("prop")).replace("busi_", BaseConstant.Separate.NONE);BusinessTypeEnum businessTypeEnum = BusinessTypeEnum.getInvBusinessTypeEnum(busiType);// 第3行——合并表头cell = row3.createCell(3+i*2);cell.setCellValue(businessTypeEnum.display());cell.setCellStyle(this.centerCellStyle);cellRangeAddress = new CellRangeAddress(2, 2, 3+i*2, 4+i*2);sheet.addMergedRegionUnsafe(cellRangeAddress);setMergedRegionStyle(sheet, cellRangeAddress);// 第4行——成本cell = row4.createCell(3+i*2);cell.setCellStyle(this.centerCellStyle);cell.setCellValue("数量");// 第4行——数量cell = row4.createCell(4+i*2);cell.setCellStyle(this.centerCellStyle);cell.setCellValue("成本");}}// 入库合计cell = row3.createCell(3+dySize*2);cell.setCellValue("入库合计");cell.setCellStyle(this.centerCellStyle);cellRangeAddress = new CellRangeAddress(2, 2, 3+dySize*2, 4+dySize*2);sheet.addMergedRegionUnsafe(cellRangeAddress);setMergedRegionStyle(sheet, cellRangeAddress);// 入库合计——成本cell = row4.createCell(3+dySize*2);cell.setCellStyle(this.centerCellStyle);cell.setCellValue("数量");// 入库合计——数量cell = row4.createCell(4+dySize*2);cell.setCellStyle(this.centerCellStyle);cell.setCellValue("成本");// 出库合计cell = row3.createCell(5+dySize*2);cell.setCellValue("出库合计");cell.setCellStyle(this.centerCellStyle);cellRangeAddress = new CellRangeAddress(2, 2, 5+dySize*2, 6+dySize*2);sheet.addMergedRegionUnsafe(cellRangeAddress);setMergedRegionStyle(sheet, cellRangeAddress);// 出库合计——成本cell = row4.createCell(5+dySize*2);cell.setCellStyle(this.centerCellStyle);cell.setCellValue("数量");// 出库合计——数量cell = row4.createCell(6+dySize*2);cell.setCellStyle(this.centerCellStyle);cell.setCellValue("成本");// 结余cell = row3.createCell(7+dySize*2);cell.setCellValue("结余");cell.setCellStyle(this.centerCellStyle);cellRangeAddress = new CellRangeAddress(2, 2, 7+dySize*2, 8+dySize*2);sheet.addMergedRegionUnsafe(cellRangeAddress);setMergedRegionStyle(sheet, cellRangeAddress);// 结余——成本cell = row4.createCell(7+dySize*2);cell.setCellStyle(this.centerCellStyle);cell.setCellValue("数量");// 结余——数量cell = row4.createCell(8+dySize*2);cell.setCellStyle(this.centerCellStyle);cell.setCellValue("成本");}

第五行是数据域

/*** 第五行:数据域* @param sheet*/private void row5(Sheet sheet){Row row = sheet.createRow(4);// 商品编码Cell cell = row.createCell(0);cell.setCellStyle(this.leftCellStyle);cell.setCellValue("{.stockCode}");// 商品名称cell = row.createCell(1);cell.setCellStyle(this.leftCellStyle);cell.setCellValue("{.stockName}");// 商品规格cell = row.createCell(2);cell.setCellStyle(this.leftCellStyle);cell.setCellValue("{.stockModel}");// 动态列int dySize = this.dynamicHeader.size();if (!CheckEmptyUtil.isEmpty(this.dynamicHeader)){for (int i=0; i<dySize; i++){Map<String,Object> colMap = this.dynamicHeader.get(i);List<Map<String,String>> chidren = (List<Map<String,String>>)colMap.get("children");// 数量Map<String,String> countMap = chidren.get(0);cell = row.createCell(3+i*2);cell.setCellStyle(this.rightCellStyle);cell.setCellValue(String.format("{.%s}", countMap.get("prop")));// 成本Map<String,String> costMap = chidren.get(1);cell = row.createCell(4+i*2);cell.setCellStyle(this.rightCellStyle);cell.setCellValue(String.format("{.%s}", costMap.get("prop")));}}// 入库合计cell = row.createCell(3+dySize*2);cell.setCellStyle(this.rightCellStyle);cell.setCellValue("{.count_total_in}");cell = row.createCell(4+dySize*2);cell.setCellStyle(this.rightCellStyle);cell.setCellValue("{.cost_total_in}");// 出库合计cell = row.createCell(5+dySize*2);cell.setCellStyle(this.rightCellStyle);cell.setCellValue("{.count_total_out}");cell = row.createCell(6+dySize*2);cell.setCellStyle(this.rightCellStyle);cell.setCellValue("{.cost_total_out}");// 结余cell = row.createCell(7+dySize*2);cell.setCellStyle(this.rightCellStyle);cell.setCellValue("{.final_count}");cell = row.createCell(8+dySize*2);cell.setCellStyle(this.rightCellStyle);cell.setCellValue("{.final_cost}");}

表格样式这里只写一个,其他的参考pos文档即可,不要每一个单元都重新创建单元格样式,那样非常消耗性能.

 private CellStyle createCellContentStyle(Workbook workbook, HorizontalAlignment align,BorderStyle borderStyle) {CellStyle style = workbook.createCellStyle();// 设置对齐样式style.setAlignment(align);//背景为白色style.setFillForegroundColor(IndexedColors.WHITE.getIndex());// 设置边框样式// 下边框style.setBorderBottom(borderStyle);// 左边框style.setBorderLeft(borderStyle);// 上边框style.setBorderTop(borderStyle);// 右边框style.setBorderRight(borderStyle);// 生成字体Font font = workbook.createFont();font.setFontName("宋体");// 设置字体大小font.setFontHeightInPoints((short) 10);// 粗体显示font.setBold(false);// 选择创建的字体格式style.setFont(font);return style;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/525160.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

苍穹外卖-day01

苍穹外卖-day01 目录 苍穹外卖-day01课程内容1. 软件开发整体介绍1.1 软件开发流程1.2 角色分工1.3 软件环境 2. 苍穹外卖项目介绍2.1 项目介绍2.2 产品原型2.3 技术选型 3. 开发环境搭建3.1 前端环境搭建3.2 后端环境搭建3.2.1 熟悉项目结构3.2.2 Git版本控制3.2.3 数据库环境…

简洁的在线观影开源项目

公众号&#xff1a;【可乐前端】&#xff0c;每天3分钟学习一个优秀的开源项目&#xff0c;分享web面试与实战知识。 每天3分钟开源 hi&#xff0c;这里是每天3分钟开源&#xff0c;很高兴又跟大家见面了&#xff0c;今天介绍的开源项目简介如下&#xff1a; 仓库名&#xff1…

伪分布Hadoop的安装与部署

1.实训目标 &#xff08;1&#xff09;熟悉掌握使用在Linux下安装JDK。 &#xff08;2&#xff09;熟悉掌握使用在Linux下安装Hadoop。 &#xff08;3&#xff09;熟悉掌握使用配置SSH免密登录。 2.实训环境与软件 环境 版本 说明 Windows 10系统 64位 操作电脑配置 …

【IEEE列表会议】IEEE第三届信息与通信工程国际会议国际会议(JCICE 2024)

会议简介 Brief Introduction 2024年第三届信息与通信工程国际会议国际会议 (JCICE 2024) 会议时间&#xff1a;2024年5月10日-12日 召开地点&#xff1a;中国福州 大会官网&#xff1a;JCICE 2024-2024 International Joint Conference on Information and Communication Engi…

筛选出等于1的式子

然后统计和归类 归类分行归类方法 算术符号归类 数字大小排序算术符号归类 import randomdef generate_expression(num_range, num_count, operators):nums random.sample(range(num_range[0], num_range[1]1), num_count)ops random.choices(operators, knum_count-1)expre…

高级语言讲义2010计专(仅高级语言部分)

1.编写一程序&#xff0c;对输入的正整数&#xff0c;求他的约数和。 如&#xff1a;18的约数和为1236939 #include <stdio.h>int getsum(int n){int i,sum0;for(i1;i<n;i)if(n%i0)sumi;return sum; } int main(){int sum getsum(18);printf("%d",sum); …

vulhub中Weblogic < 10.3.6 ‘wls-wsat‘ XMLDecoder 反序列化漏洞(CVE-2017-10271)复现

Weblogic的WLS Security组件对外提供webservice服务&#xff0c;其中使用了XMLDecoder来解析用户传入的XML数据&#xff0c;在解析的过程中出现反序列化漏洞&#xff0c;导致可执行任意命令。 访问http://your-ip:7001/即可看到一个404页面&#xff0c;说明weblogic已成功启动 …

【数据结构】用栈实现队列

前言&#xff1a;本节博客分享了用栈实现队列效果的思路以及代码&#xff0c;有需要借鉴即可。 1.题目及链接 LINK 2.思路分析 如果要用栈实现队列&#xff0c;我们直到栈是先入后出的一个效果&#xff0c;所以我们可以用两个栈&#xff0c;这样逆转两次数不就是入栈之前数组…

深入理解神经网络

图片怎么被识别的过程 (每层神经网络是数组,会对进来的数据进行加权求和[(weight*数据 然后累加) bias])(激活函数是为了训练weight和bias偏移值,在每个神经网络)(分类器会统计概率分类) 2. 引用链接 https://mp.weixin.qq.com/s?__bizMzIyNjMxOTY0NA&mid2247500124&…

FFmpeg--AAC音频解码流程

文章目录 AAC 组成函数分析读aac帧写aac帧aac的head参数设置 运行结果 AAC 组成 AAC音频格式&#xff1a;是⼀种由MPEG-4标准定义的有损⾳频压缩格式 ADTS:是AAC音频的传输流格式 AAC音频文件的每一帧由ADTS Header和AAC Audio Data组成 每⼀帧的ADTS的头⽂件都包含了⾳频的采…

STL容器之哈希的补充——其他哈希问题

1.其他哈希问题 ​ 减少了空间的消耗&#xff1b; 1.1位图 ​ 位图判断在不在的时间复杂度是O(1)&#xff0c;速度特别快; ​ 使用哈希函数直接定址法&#xff0c;1对1映射&#xff1b; ​ 对于海量的数据判断在不在的问题&#xff0c;使用之前的一些结构已经无法满足&…

13. 用户注册功能实现

文章目录 一 、增加路由二、书写流程控制&#xff08;controller&#xff09;逻辑三、书写业务逻辑四、与DB交互五、测试 代码地址&#xff1a;https://gitee.com/lymgoforIT/bluebell 一 、增加路由 添加路由&#xff0c;使用分组管理 v1 : r.Group("/api/v1")//…