JAVA excel合并单元格原生poi

合并后的效果

java csv文件单元格合并 java excel合并单元格样式_垂直居中


直接上代码 (该方法为如果指定行的单元格里面的值一致则进行合并,直接粘贴使用即可)

*
     * @param sheet
     * @param colIdx	合并的列
     * @param startRow	起始行
     * @param stopRow	结束行
     * @param isForward	是否递进合并其它列
     * @param forwardToColIdx 递进到的列
     */
    public void mergeRowCell(HSSFSheet sheet,int colIdx,int startRow,int stopRow ,boolean isForward,int forwardToColIdx){
        String compareValue = null;
        int beginRow = startRow;
        int endRow = startRow;
        for(int i=startRow;i<=stopRow; ++i){
            String value = sheet.getRow(i).getCell(colIdx).getRichStringCellValue()==null ? "":sheet.getRow(i).getCell(colIdx).getRichStringCellValue().toString();
            if(i == startRow){
                compareValue = value;
            }else{
                if(compareValue.equals(value)){//相同,则设置重复的值为空
                    sheet.getRow(i).getCell(colIdx).setCellValue("");
                    endRow = i;
                }else {//不同,则合并之前相同的单元格
                    if(beginRow < endRow){
                        CellRangeAddress cellRangeAddress = new CellRangeAddress(beginRow, endRow, colIdx, colIdx);
                        sheet.addMergedRegion(cellRangeAddress);
                        HSSFCellStyle jz = workbook.createCellStyle();//新建单元格样式
                        jz.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
                        jz.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
                        if(isForward){//递进合并下一列
                            int nextColIndex = colIdx;
                            if(colIdx < forwardToColIdx){
                                nextColIndex ++;
                            }else if(colIdx > forwardToColIdx){
                                nextColIndex --;
                            }else{
                                return;
                            }
                            mergeRowCell(sheet, nextColIndex, beginRow, endRow, isForward, forwardToColIdx);
                        }
                    }

                    compareValue = value;
                    beginRow = i;
                    endRow = i;
                }
            }

        }
        if(beginRow < endRow){
            CellRangeAddress cellRangeAddress = new CellRangeAddress(beginRow, endRow, colIdx, colIdx);
            sheet.addMergedRegion(cellRangeAddress);
            HSSFCellStyle jz = workbook.createCellStyle();//新建单元格样式
            jz.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
            jz.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
            if(isForward){//递进合并下一列
                int nextColIndex = colIdx;
                if(colIdx < forwardToColIdx){
                    nextColIndex ++;
                }else if(colIdx > forwardToColIdx){
                    nextColIndex --;
                }else{
                    return;
                }
                mergeRowCell(sheet, nextColIndex, beginRow, endRow, isForward, forwardToColIdx);
            }
        }
    }

在这里插入代码片

public ResponseEntity  InspectionReport(){
	创建sheet
	workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("sheet");
    创建行
    HSSFRow row4 = sheet.createRow(1);
    生成表头
    List<String> objects = new ArrayList<String>();
            objects.add("序号");
            objects.add("姓名");
            objects.add("年龄");
            objects.add("等级");
            objects.add("职位");
            objects.add("性别");
            objects.add("创建时间");
            objects.add("识别时间");
  FileInputStream inputStream = null;
  List<ReportEntity> list=testService.findAllStudent();           
  if (CollectionUtils.isNotEmpty(list)) {
                int num = 1;
                for (int i = 0; i < list.size(); i++) {

                    ReportEntity potrolTask = list.get(i);
                    //potrolTask.
                    HSSFRow row5 = sheet.createRow(num);
                    row5.createCell(0).setCellValue(potrolTask.getInterval());
                    row5.createCell(1).setCellValue(potrolTask.getEquipment());
                    row5.createCell(2).setCellValue(potrolTask.getComponent());
                    row5.createCell(3).setCellValue(potrolTask.getPoint());
                    String alarmType = potrolTask.getAlarmType();
                    row5.createCell(4).setCellValue(potrolTask.getResult());
                    row5.createCell(6).setCellValue(potrolTask.getMessage());
                    row5.createCell(7).setCellValue(potrolTask.getSureTime());
                    row5.createCell(8).setCellValue(potrolTask.getType());
                        num++;
      	 }
      }
           
            //调用方法合并单元格
     	for(int i=0;i<3;i++){
           mergeRowCell(sheet,i, 1, list.size()+1, false, 0);
       }
}


大概就是这样了,后期会更新 导出csv或excel的工具类简单实用,喜欢刘关注我吧,本人博客只针对工作中用到的技术点进行更新