POI 动态合并单元格

来源:互联网 发布:sql 左联 编辑:程序博客网 时间:2024/04/25 15:08
  所谓动态合并,要明白,怎样才能合并,是根据id还是根据其他的,比如根据id,要想合并,
  那么要合并的两项的数据必须挨着,那么问题就在于sql 怎么去写.
  因此,动态合并的主要还是在sql, order by XXX 
/ *****查询结果 ****** // ** *报表导出 *:  * @throws异常 * /public void queryExcelActionInfo(OutputStream out,Map <String,Object>参数)throws Exception {List <ActionCount> queryActionInfo = actionCountMapper.queryExcelReportInfo(parameters);/ ****************************************** POI ****** ********************************************* /System.out.println(“============================================== =================“);HSSFWorkbook book = new HSSFWorkbook();HSSFSheet sheet = book.createSheet(“口腔saas操作统计”);for(int i = 0; i <18; i ++){sheet.setColumnWidth(i,4000);}}/ *****************************设置表头***************** ****************** /HSSFRow row1 = sheet.createRow(0);//此处可以 循环创建cellHSSFCell cell1 = row1.createCell(0),cell2 = row1.createCell(1),cell3 = row1.createCell(2),cell4 = row1.createCell(3),cell5 = row1.createCell(4),cell6 = row1.createCell(5),cell7 = row1.createCell(6),cell8 = row1.createCell(7),cell9 = row1.createCell(8),cell10 = row1.createCell(9),cell11 = row1.createCell(16),cell18 = row1.createCell(17);cell1.setCellValue(“序号”);cell2.setCellValue(“名称”);cell3.setCellValue(“时间”);cell4.setCellValue(“区域(地址)”);cell5.setCellValue(“姓名”);cell6.setCellValue(“手机号”);cell7.setCellValue(“角色”);cell8.setCellValue(“时间统计”);cell9.setCellValue(“00模块使用次数”);cell10.setCellValue(“模块使用次数”);/ ***********************循环创建表格*********************** *************** /int count = 0; //合并序号for(int i = 0; i <queryActionInfo.size(); i ++){HSSFRow row = sheet.createRow((int)i + 1);ActionCount actionCount = queryActionInfo.get(i);//创建单元格并设置值if(null!= actionCount.getOrgName()){row.createCell(1).setCellValue(actionCount.getOrgName());}}if(null!= actionCount.getInstalltime()){String string = DateUtil.StringToString(actionCount.getInstalltime(),“yyyy-MM-dd”);row.createCell(2).setCellValue(string);}}if(null!= actionCount.getAddress()){row.createCell(3).setCellValue(actionCount.getAddress());}}if(null!= actionCount.getUsername()){row.createCell(4).setCellValue(actionCount.getUsername());}}if(null!= actionCount.getUserphone()){row.createCell(5).setCellValue(actionCount.getUserphone());}}if(null!= actionCount.getUserRole()){row.createCell(6).setCellValue(actionCount.getUserRole());}}if(null!= actionCount.getSc()){int j =(int)Double.parseDouble(actionCount.getSc());String sc = DateUtil.secToTime(j);row.createCell(7).setCellValue(sc);}其他{row.createCell(7).setCellValue(“00:00”);}}if(null!= actionCount.getPatientCount()){row.createCell(8).setCellValue(actionCount.getPatientCount()+“次”);} else {row.createCell(8).setCellValue(“0次”);}}if(null!= actionCount.getAppointCount()){row.createCell(9).setCellValue(actionCount.getAppointCount()+“次”);} else {row.createCell(9).setCellValue(“0次”);}}if(i!= 0){//比较id如果一样则合并if(queryActionInfo.get(i-1).getOrgID().equals(queryActionInfo.get(i).getOrgID())){/ * sheet.addMergedRegion(new CellRangeAddress(1,1,0,0));   参数一:起始行,参数二:结束行,参数三:起始列,参数四:结束列
   此处可以循环进行合并
* /sheet.addMergedRegion(new CellRangeAddress(i,1 + i,0,0));sheet.addMergedRegion(new CellRangeAddress(i,1 + i,1,1));sheet.addMergedRegion(new CellRangeAddress(i,1 + i,2,2));sheet.addMergedRegion(new CellRangeAddress(i,1 + i,3,3));   count ++;}} }}row.createCell(0).setCellValue(Integer.parseInt(actionCount.getRowNo() -  count));}}book.write(out);}}
                                             
0 0