How to determine number of rows in a merged cell using apache poi? How to determine number of rows in a merged cell using apache poi? selenium selenium

How to determine number of rows in a merged cell using apache poi?


The below code will determine the number of merged cells in a column - colName, with begining row as startingRow

public int getNumOfMergedRows(String colName, int startingRow) {    int rowsMerged = 0, col = 0;    XSSFRow mergedRow = null;    XSSFCell mergedCell = null;    try {        col = getExcelColNum(colName);        for (int i = startingRow + 1; i < sheet.getPhysicalNumberOfRows(); i++) {            mergedRow = sheet.getRow(i);            mergedCell = mergedRow.getCell(col);            if (mergedCell.getCellTypeEnum() == null || mergedCell.getCellTypeEnum() == CellType.BLANK)                rowsMerged++;            else                break;        }        rowsMerged++;    }    catch (Exception e) {        e.printStackTrace();    }    logger.info(rowsMerged + " rows are merged in columne" + colName + " for " + scriptName + " script");    return rowsMerged;}


I have a sample code which takes the fiel as input.It will count the values in 3rd column for all values and stores it in a map. It does so by counting the blank values in excel file and reset the counter at each valid value

import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellType;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class Demo {    public static void main(String[] args) {        readFile(new File("excelstack.xlsx"));    }    public static void readFile(File file) {        try {            FileInputStream excelFile = new FileInputStream(file);            Workbook workbook = new XSSFWorkbook(excelFile);            Sheet datatypeSheet = workbook.getSheetAt(0);            Iterator<Row> iterator = datatypeSheet.iterator();            Map<String,Integer> map=new HashMap<String,Integer>();            int coltocheck=1;            int counter=1;            String currentitem="";            while (iterator.hasNext()) {                Row currentRow = iterator.next();                Iterator<Cell> cellIterator = currentRow.iterator();                                                              while (cellIterator.hasNext()) {                    Cell currentCell = cellIterator.next();                                                     if(currentCell.getCellTypeEnum()==CellType.BLANK ) {                             if(currentCell.getColumnIndex()==coltocheck)                                    map.put(currentitem, counter++);                    }                                        else if(currentCell.getCellTypeEnum()==CellType.STRING && currentCell.getColumnIndex()==coltocheck) {                            currentitem=currentCell.getStringCellValue();                                                   counter=1;                            map.put(currentitem, counter++);                    }                }                                                            }            for(Map.Entry<String, Integer> m:map.entrySet())               System.out.println(m.getKey()+" "+m.getValue());        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {                             }    }}