使用EasyPOI怎么实现动态生成列数
发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,使用EasyPOI怎么实现动态生成列数,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。准备 cn.afte
千家信息网最后更新 2025年12月01日使用EasyPOI怎么实现动态生成列数准备
使用EasyPOI怎么实现动态生成列数,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
准备
cn.afterturn easypoi-base 3.2.0 org.slf4j slf4j-api org.apache.commons commons-lang3 cn.afterturn easypoi-annotation 3.2.0
四、详细步骤
定义表格样式
/** * 定义表格样式 * * @param start 查询起始日期 * @param end 查询结束日期 * @return java.util.List* @author huan * @date 2019/6/21 * @since 2.8.2 */ private List setExportExcelStyle(DateTime start, DateTime end) { //定义表格列名,该集合存放的就是表格的列明,每个对象就是表格中的一列 List modelList = new ArrayList (); //该对象就是定义列属性的对象 ExcelExportEntity excelentity = null; //定义第一个列 excelentity = new ExcelExportEntity("企业全称", "companyName"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第二个列 excelentity = new ExcelExportEntity("企业简称", "companyShortName"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第三个列,这里指定了日期显示格式 excelentity = new ExcelExportEntity("认证日期", "openDate"); excelentity.setWidth(20); excelentity.setHeight(10); excelentity.setFormat("yyyy-MM-dd"); modelList.add(excelentity); //定义第四个列,这边就是动态生成的,跟用用户选择的日期范围,动态生成列的数量 excelentity = new ExcelExportEntity(null, "recordDate"); //设置一个集合,存放动态生成的列 List modelListChild = new ArrayList (); start = DateUtils.getDateZeroTime(start); while (start.isBefore(end)) { String date = start.toString("yyyy-MM-dd"); modelListChild.add(new ExcelExportEntity(date, date, 15)); start = start.plusDays(1); } //日期按从小到大顺序排序,这里用了最简单的冒泡排序 for (int i = 0; i < modelListChild.size(); i++) { for (int j = 0; j < modelListChild.size(); j++) { String e1 = modelListChild.get(i).getKey().toString(); String e2 = modelListChild.get(j).getKey().toString(); if (e1.compareTo(e2) < 0) { ExcelExportEntity x1 = modelListChild.get(i); ExcelExportEntity x2 = modelListChild.get(j); modelListChild.set(j, x1); modelListChild.set(i, x2); } } } //将定义好的字列放到父列中 excelentity.setList(modelListChild); modelList.add(excelentity); //定义第五个列 excelentity = new ExcelExportEntity("应当使用天数", "shouldUseDay"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第六个列 excelentity = new ExcelExportEntity("实际使用天数", "actualUseDay"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第七个列 excelentity = new ExcelExportEntity("使用率", "rate"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第八个列 excelentity = new ExcelExportEntity("推荐人", "commandMan"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第九个列 excelentity = new ExcelExportEntity("拓客", "tk"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); //定义第十个列 excelentity = new ExcelExportEntity("对接人", "connector"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); return modelList; }
定义表格数据
private List
主体方法
/** * 报表导出 * * @param analyseStockQuery analyseStockQuery * @param response response * @return javax.servlet.http.HttpServletResponse * @author huan * @date 2019/6/21 * @since 2.8.2 */ public HttpServletResponse exportStock(AnalyseStockQuery analyseStockQuery, HttpServletResponse response) { try { //设置默认查询日期 analyseStockQuery = setDefaultQueryDate(analyseStockQuery); //参数校验 checkListDetailDataParam(analyseStockQuery); //日期格式化 DateTime start = new DateTime().withDate(new LocalDate(analyseStockQuery.getQueryStartDate())); DateTime end = new DateTime().withDate(new LocalDate(analyseStockQuery.getQueryLastDate())); //定义表格样式 List modelList = setExportExcelStyle(start, end); //定义表格名称 String fileName = URLEncoder.encode("客户库存使用统计表-" + start.toString("yyyy年MM月dd日") + "~" + end.toString("yyyy年MM月dd日"), "utf-8"); // Sheet1样式 ExportParams sheet1ExportParams = new ExportParams(); // 设置sheet得名称 sheet1ExportParams.setSheetName("入库统计"); // 创建sheet1使用得map Map sheet1ExportMap = new HashMap<>(); // title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName sheet1ExportMap.put("title", sheet1ExportParams); //sheet1样式 sheet1ExportMap.put("entityList", modelList); //sheet1中要填充得数据,true表示查询入库数据,false表示查询易签待入库数据 sheet1ExportMap.put("data", getData(analyseStockQuery, true)); //Sheet2设置 ExportParams sheet2ExportParams = new ExportParams(); sheet2ExportParams.setSheetName("易签待入库统计"); Map sheet2ExportMap = new HashMap<>(); sheet2ExportMap.put("title", sheet2ExportParams); sheet2ExportMap.put("entityList", modelList); sheet2ExportMap.put("data", getData(analyseStockQuery, false)); // 将sheet1、sheet2使用得map进行包装 List> sheetsList = new ArrayList<>(); sheetsList.add(sheet1ExportMap); sheetsList.add(sheet2ExportMap); // 执行方法 Workbook workBook = exportExcel(sheetsList, ExcelType.HSSF); //设置response response.setHeader("content-disposition", "attachment;filename=" + fileName + ".xls"); //设置编码格式 response.setCharacterEncoding("GBK"); //将表格内容写到输出流中并刷新缓存 @Cleanup ServletOutputStream out = response.getOutputStream(); workBook.write(out); out.flush(); workBook.close(); } catch (FileNotFoundException e) { log.debug("FileNotFoundException:{}", e.getMessage()); } catch (UnsupportedEncodingException e) { log.debug("UnsupportedEncodingException:{}", e.getMessage()); } catch (IOException e) { log.debug("IOException:{}", e.getMessage()); } return response; } 看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
表格
数据
日期
样式
生成
查询
动态
就是
名称
对象
格式
排序
统计
一行
三个
企业
内容
参数
天数
方法
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
技术成果大数据库
软件开发专业技能怎么写
国际对网络安全的研究
北邮网络技术学院编程软件
高校网络安全有哪些文献
我的世界tls服务器最老实的人
育碧服务器失去链接图片
星辰mega服务器的密码
如何查看服务器密码有效期
福州大学万德数据库
网络安全教育班级评价
网络安全属于民生保障吗
网络安全主动与被动
找靓机网络服务器
如何结合教学工作做好网络安全
北京正规软件开发品质保障
常用服务器的固定端口是什么
删除mysql数据库用户
高级数据库技术难吗
计算机网络安全认证视频
服务器弱口令
魔抗世界任务网络安全
深圳戴尔服务器厂家报价
周口城尚互联网科技有限公司
网络安全刘建伟pdf
数据库从入门到进阶解压密码
为什么软件开发比后台工资低
数据库不能启动3414
软件开发保密条款
联想 x86服务器