使用EasyPOI怎么实现动态生成列数,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.2.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> <exclusion> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.2.0</version> </dependency>
定义表格样式
/** * 定义表格样式 * * @param start 查询起始日期 * @param end 查询结束日期 * @return java.util.List<cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity> * @author huan * @date 2019/6/21 * @since 2.8.2 */ private List<ExcelExportEntity> setExportExcelStyle(DateTime start, DateTime end) { //定义表格列名,该集合存放的就是表格的列明,每个对象就是表格中的一列 List<ExcelExportEntity> modelList = new ArrayList<ExcelExportEntity>(); //该对象就是定义列属性的对象 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<ExcelExportEntity> modelListChild = new ArrayList<ExcelExportEntity>(); 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<Map<String, Object>> getData(AnalyseStockQuery analyseStockQuery, boolean type) { //获取数据源 ArrayList<AnalyseStockExportDto> dtoList = listDetailDataWithNum(analyseStockQuery, type); List<Map<String, Object>> dataList = new ArrayList<>(); //存储没一行中的日期数据 List<Map<String, Object>> dataListChild = null; //存储表格中的每一行数据 Map<String, Object> mapParent = null; //数据排序 dtoList.sort(new ExportComparator()); //定义表格数据 for (AnalyseStockExportDto dto : dtoList) { mapParent = new HashMap(7); //这边只要和定义表格样式的时候 名称一致就行 我这边因为有三个字段不需要我这边后台生成,所以就没有设置默认值了 mapParent.put("companyName", dto.getCompanyName()); mapParent.put("companyShortName", dto.getCompanyShortName()); mapParent.put("openDate", dto.getOpenDate()); mapParent.put("shouldUseDay", dto.getShouldUseDay()); mapParent.put("actualUseDay", dto.getActualUseDay()); mapParent.put("rate", dto.getRate()); Map<String, Object> map = dto.getDateList(); dataListChild = new ArrayList<>(); dataListChild.add(map); mapParent.put("recordDate", dataListChild); dataList.add(mapParent); } return dataList; }
主体方法
/** * 报表导出 * * @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<ExcelExportEntity> 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<String, Object> 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<String, Object> sheet2ExportMap = new HashMap<>(); sheet2ExportMap.put("title", sheet2ExportParams); sheet2ExportMap.put("entityList", modelList); sheet2ExportMap.put("data", getData(analyseStockQuery, false)); // 将sheet1、sheet2使用得map进行包装 List<Map<String, Object>> 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; }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。