行中获取下一个单元格

行中获取下一个单元格

本文介绍了Java的Excel中:行中获取下一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个字符串被发现后,得到了下一个单元格的值。比方说,我得到A1为包含这里我的搜索字符串的单元格,我试图让下一个单元格中的内容A1后,在行中。

 私有静态字符串findRow(XSSFSheet片,串cellContent){
            对于(鳞次栉比:表){
                (电池单元:行){
                    如果(cell.getCellType()== Cell.CELL_TYPE_STRING){
                        如果(cell.getRichStringCellValue()的getString()。修剪()。等于(cellContent)){
                            电池= cell.getNextRowCell //弥补方法
                            返回cell.getStringCellValue();                        }
                    }
            }


解决方案

 我想通了。私人静态字符串getNextRow(XSSFSheet片,串cellContent){
        迭代器<行> ITR = sheet.iterator();
        而(itr.hasNext()){
            鳞次栉比= itr.next();
            迭代&所述;电池> cellIterator = row.cellIterator();
            而(cellIterator.hasNext()){
                电池单元= cellIterator.next();
                如果(cell.getCellType()== Cell.CELL_TYPE_STRING){
                    如果(cell.getRichStringCellValue()的getString()。修剪()
                            .equals(cellContent)){
                        行= itr.next();
                        INT VAL = cell.getColumnIndex();
                        细胞= row.getCell(VAL);
                        串srow = cell.getStringCellValue();
                        返回srow;                    }                }            }
        }        返回null;
    }

I am trying to get the value of the next cell in a after a string is found. Let's say I get A1 as the cell that contains my search string "here", I am trying to get the contents of the next cell after A1 in the row.

 private static String findRow(XSSFSheet sheet, String cellContent) {
            for (Row row : sheet) {
                for (Cell cell : row) {
                    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                        if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) {
                            cell = cell.getNextRowCell //made up method
                            return cell.getStringCellValue();

                        }
                    }
            }
解决方案
I figured it out.

private static String getNextRow(XSSFSheet sheet, String cellContent) {
        Iterator<Row> itr = sheet.iterator();
        while (itr.hasNext()) {
            Row row = itr.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    if (cell.getRichStringCellValue().getString().trim()
                            .equals(cellContent)) {
                        row = itr.next();
                        int val = cell.getColumnIndex();
                        cell = row.getCell(val);
                        String srow = cell.getStringCellValue();
                        return srow;

                    }

                }

            }
        }

        return null;
    }

这篇关于Java的Excel中:行中获取下一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:27