Excel导出后合并单元格

/** 
       * 合并单元格 
       * @param sheet 要合并单元格的excel 的sheet
       * @param cellLine  要合并的列 
       * @param startRow  要合并列的开始行 
       * @param endRow    要合并列的结束行 
       */
   //addMergedRegion(sheet, 0, 0, sheet.getLastRowNum(),workBook);合并第一列的所有相同单元格
      public static void addMergedRegion(HSSFSheet sheet, int cellLine, int startRow, int endRow,HSSFWorkbook workBook){   //合并列
            
       HSSFCellStyle style0 = workBook.createCellStyle(); // 样式对象
      
         style0.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  // 垂直  
         style0.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平 
         // 设置边框  
         style0.setBottomBorderColor(HSSFColor.BLACK.index);  
         style0.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
         style0.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
         style0.setBorderRight(HSSFCellStyle.BORDER_THIN);  
         style0.setBorderTop(HSSFCellStyle.BORDER_THIN);
      
         style0.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);  //填充色:淡绿色
       style0.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
     
         
          //获取第一行的数据,以便后面进行比较   
          String s_will = sheet.getRow(startRow).getCell(cellLine).getStringCellValue();  
      
          int count = 0;
          boolean flag = false;
          for (int i = 1; i <= endRow; i++) {  
           String s_current = sheet.getRow(i).getCell(0).getStringCellValue();
           if(s_will.equals(s_current)){
              s_will = s_current;
              if(flag)
              {
                sheet.addMergedRegion(new CellRangeAddress(startRow-count,startRow,cellLine,cellLine));
                HSSFRow row = sheet.getRow(startRow-count);
                String cellValueTemp = sheet.getRow(startRow-count).getCell(0).getStringCellValue();
                HSSFCell cell = row.createCell(0);
                cell.setCellValue(cellValueTemp); // 跨单元格显示的数据   
                cell.setCellStyle(style0); // 样式   
                count = 0;
                flag = false;
              }
              startRow=i;
              count++;         
           }else{
              flag = true;
              s_will = s_current;
           }
    //由于上面循环中合并的单元放在有下一次相同单元格的时候做的,所以最后如果几行有相同单元格则要运行下面的合并单元格。
           if(i==endRow&&count>0)
            {
              sheet.addMergedRegion(new CellRangeAddress(endRow-count,endRow,cellLine,cellLine));  
              String cellValueTemp = sheet.getRow(startRow-count).getCell(0).getStringCellValue();
              HSSFRow row = sheet.getRow(startRow-count);
              HSSFCell cell = row.createCell(0);
              cell.setCellValue(cellValueTemp); // 跨单元格显示的数据   
                 cell.setCellStyle(style0); // 样式   
            }
          }
      }

 

 

 

 // addMergedRegion2(sheet, 0, 0, listzl.size(),wb);//合并第一行的所有相同单元格
      public static void addMergedRegion2(HSSFSheet sheet, int rowLine, int startCell, int endCell,HSSFWorkbook workBook){   //合并行
     // 创建字体样式  
       HSSFFont font = workBook.createFont();  
       font.setColor(HSSFColor.RED.index);  //字体蓝色
       /////////////////////////////////////////////////////////////////////////////////////
      //设置单元格样式  
       HSSFCellStyle style = workBook.createCellStyle();
       style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
       style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
       // 设置边框  
       style.setBottomBorderColor(HSSFColor.BLACK.index);  
       style.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
       style.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
       style.setBorderRight(HSSFCellStyle.BORDER_THIN);  
       style.setBorderTop(HSSFCellStyle.BORDER_THIN);
      
       style.setFont(font);// 设置字体
      
      
       //获取第一行的数据,以便后面进行比较   
          String s_will = sheet.getRow(rowLine).getCell(startCell).getStringCellValue();  
      
          int count = 0;
          boolean flag = false;
          for (int i = 1; i <= endCell; i++) {  
           String s_current = sheet.getRow(0).getCell(i).getStringCellValue();
           if(s_will.equals(s_current)){
              s_will = s_current;
              if(flag)
              {
                sheet.addMergedRegion(new CellRangeAddress(rowLine,rowLine,startCell-count,startCell));
                HSSFRow row = sheet.getRow(0);
                String cellValueTemp = sheet.getRow(0).getCell(startCell-count).getStringCellValue();
                HSSFCell cell = row.createCell(startCell-count);
                cell.setCellValue(cellValueTemp); // 跨单元格显示的数据   
                cell.setCellStyle(style); // 样式   
                count = 0;
                flag = false;
              }
              startCell=i;
              count++;         
           }else{
              flag = true;
              s_will = s_current;
           }
    //由于上面循环中合并的单元放在有下一次相同单元格的时候做的,所以最后如果几行有相同单元格则要运行下面的合并单元格。
           if(i==endCell&&count>0)
            {
              sheet.addMergedRegion(new CellRangeAddress(rowLine,rowLine,endCell-count,endCell));  
              String cellValueTemp = sheet.getRow(0).getCell(startCell-count).getStringCellValue();
              HSSFRow row = sheet.getRow(0);
              HSSFCell cell = row.createCell(startCell-count);
              cell.setCellValue(cellValueTemp); // 跨单元格显示的数据   
                 cell.setCellStyle(style); // 样式   
            }
          }
      }

你可能感兴趣的:(Excel)