poi 多行合并

时间:2025-04-05 09:16:19
public static void test() { String beginTime = "2017-10-08"; String endTime = "2017-10-11"; HSSFWorkbook wb = new HSSFWorkbook(); Date b = (beginTime, "yyyy-MM-dd"); Date e = (endTime, "yyyy-MM-dd"); String bs = (b, ""); String es = (e, ""); String sheetName = bs + "-" + es; HSSFSheet sheet = (sheetName); HSSFRow row = ((short) 0);// 第一行 // 定制表头 List<String> header = new ArrayList<>(); ("部门"); ("岗位"); ("员工编号"); ("姓名"); ("服务中心名称"); ("时间段"); ("次数"); ("走访日期"); ("到店时段"); ("时长(分钟)"); HSSFCellStyle style = (); (HSSFCellStyle.ALIGN_LEFT);// 水平居中 (HSSFCellStyle.VERTICAL_CENTER);// 垂直居中 (true);// 自动换行 ((short) 5);// 缩进 for (int i = 0; i < (); i++) { HSSFCell cell = (i); ((i)); } // List<OaPunchVisitDataDto> // dtos=(beginTime, endTime, departName, // name); List<OaPunchVisitDataDto> dtos = (); OaPunchVisitDataDto dto = new OaPunchVisitDataDto(); ("张三"); ("开发部"); dto.setLOCATION_TITLE("五里店"); ("10000"); ("员工"); dto.setCHECK_TIME("2017-10-09"); dto.setCHECK_ATS("2017-10-09 09:54,2017-10-09 17:54"); OaPunchVisitDataDto dto1 = new OaPunchVisitDataDto(); ("张三"); ("开发部"); dto1.setLOCATION_TITLE("五里店1"); ("10000"); ("员工"); dto1.setCHECK_TIME("2017-10-10"); dto1.setCHECK_ATS("2017-10-10 09:54,2017-10-10 17:54"); OaPunchVisitDataDto dto3 = new OaPunchVisitDataDto(); ("张三2"); ("开发部"); dto3.setLOCATION_TITLE("五里店"); ("10001"); ("员工"); dto3.setCHECK_TIME("2017-10-10,2017-10-11"); dto3.setCHECK_ATS("2017-10-10 09:54,2017-10-10 17:54,2017-10-11 09:54,2017-10-11 17:54"); OaPunchVisitDataDto dto5 = new OaPunchVisitDataDto(); ("张三2"); ("开发部"); dto5.setLOCATION_TITLE("五里店1"); ("10001"); ("员工"); dto5.setCHECK_TIME("2017-10-08"); dto5.setCHECK_ATS("2017-10-08 09:54,2017-10-08 17:54"); (dto); (dto1); (dto3); (dto5); Multimap<String, OaPunchVisitDataDto> multimap = (); Set<String> keySet = new HashSet<>(); for (OaPunchVisitDataDto data : dtos) { (()); ((), data); } // 数据行----合并数据最多行 if (keySet != null && () > 0) { int i = 0; int temp = 0; for (String key : keySet) { int maxRow = 0; for (OaPunchVisitDataDto a : (key)) { // 获取最大合并行数 if (a.getCHECK_TIME() != null) { maxRow += (a.getCHECK_TIME().split(",")).size(); } } for (int c = 1 + temp; c < i + 1 + maxRow; c++) {// 4 hang HSSFRow rows = (c);// for (int x = 0; x < (); x++) { (x); ("row:"+c+",cloum:"+x); ("row:"+c+",cloum:"+x); } } temp = i + maxRow; i = i + maxRow; } // 赋值 int xx = 0; int cloumcount5 = 0;// 第五列计数 int cloumcount7 = 0; for (String key : keySet) { int maxRow = 0; Multimap<String,OaPunchSiteDataTempDto> localtionsMap = ();//访问日期集合 Set<String> localtions = ();// 访问位置集合 for (OaPunchVisitDataDto a : (key)) { // 获取最大合并行数 if (a.getCHECK_TIME() != null) { maxRow += (a.getCHECK_TIME().split(",")).size(); } List<String> visitDates = (); if (a.getCHECK_TIME() != null) { visitDates = (a.getCHECK_TIME().split(",")); } OaPunchSiteDataTempDto tdto=new OaPunchSiteDataTempDto(); (visitDates); (a.getCHECK_ATS()); // 用于确定第5列(服务中心)需要合并的行数 (a.getLOCATION_TITLE(), tdto); (a.getLOCATION_TITLE()); } String departname = (key).iterator().next().getDEPARTNAME() == null ? "" : (key).iterator().next().getDEPARTNAME(); String position = (key).iterator().next().getPOSITION() == null ? "" : (key).iterator().next().getPOSITION(); String userid = key; String username = (key).iterator().next().getNAMES() == null ? "" : (key).iterator().next().getNAMES(); ("xx+1:"+(xx+1)); HSSFCell cell0 = (xx + 1).getCell(0); HSSFCell cell1 = (xx + 1).getCell(1); HSSFCell cell2 = (xx + 1).getCell(2); HSSFCell cell3 = (xx + 1).getCell(3); (departname); (position); (userid); (username); (style); (style); (style); (style); /** * 合并前4列 */ ("xx+1:"+(xx+1)); ("xx + 1+maxRow:"+(xx + 1+maxRow)); /** * 合并前4列 */ ( new CellRangeAddress((xx + 1), (xx+maxRow), 0, 0) ); ( new CellRangeAddress((xx + 1), (xx+maxRow), 1, 1) ); ( new CellRangeAddress((xx + 1), (xx+maxRow), 2, 2) ); ( new CellRangeAddress((xx + 1), (xx+maxRow), 3, 3) ); for (String localtionKey : localtions) { int size = (localtionKey).iterator().next().getDates().size(); //第5列进行赋值 HSSFCell cell4 = (cloumcount5 + 1).getCell(4); (localtionKey); (style); //第6列进行赋值 HSSFCell cell5 = (cloumcount5 + 1).getCell(5); (beginTime+"--"+endTime); //(style); //第七列进行赋值 HSSFCell cell6 = (cloumcount5 + 1).getCell(6); (size); (style); /** * 合并5--7列数据 */ ( new CellRangeAddress(cloumcount5 + 1, cloumcount5+size, 4, 4) ); ( new CellRangeAddress(cloumcount5 + 1, cloumcount5+size, 5, 5) ); ( new CellRangeAddress(cloumcount5 + 1, cloumcount5+size, 6, 6) ); ("size:"+size); cloumcount5 = cloumcount5 + size; Iterator<OaPunchSiteDataTempDto> iterator = (localtionKey).iterator(); int m = 0; while (()) { OaPunchSiteDataTempDto po=(); List<String> visitDates = (); String[] visitDatesAts= ().split(","); for (String visitDate : visitDates) { HSSFCell cell7 = ((m + 1 + cloumcount7)).getCell(7); (visitDate); (style); Map<String, String> map=(visitDate, visitDatesAts); HSSFCell cell8 = ((m + 1 + cloumcount7)).getCell(8); (("timeAt")); (style); HSSFCell cell9 = ((m + 1 + cloumcount7)).getCell(9); (("min")); (style); m++; } } cloumcount7 = cloumcount7 + size; } xx = xx + maxRow; } (20); FileOutputStream fileOut; try { fileOut = new FileOutputStream("f://"); (fileOut); (); } catch (Exception e1) { (); } ("OK"); } }