使用该位置找到另一个小区

使用该位置找到另一个小区

本文介绍了Java的POI:如何找到一个字符串值的Excel单元格并获取其位置(行)使用该位置找到另一个小区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个小区中为具有字符串总计,然后使用在其中该单元是行找到另一个细胞,其始终是相同的细胞/列的总价值(preadsheet在0基于索引的第10单元)。

我有以下的code,它没有错误(语法),但findCell方法没有返回值的rowNum:

 公共静态无效的主要(字串[] args)抛出IOException        字符串文件名=C:\\\\文件路径\\\\的report.xls
        字符串cellContent =总计;
        INT rownr = 0,colnr = 10;        输入的InputStream =新的FileInputStream(文件名);        HSSFWorkbook WB =新HSSFWorkbook(输入);
        HSSFSheet片= wb.getSheetAt(0);        rownr = findRow(片材,cellContent);        输出(片材,rownr,colnr);        完();
    }    私有静态无效输出(HSSFSheet片,诠释rownr,诠释colnr){
        / *
         *此方法显示本月的合计值
         * /        HSSFRow行= sheet.getRow(rownr);
        HSSFCell细胞= row.getCell(colnr);                的System.out.println(您一共是:+细胞);
    }    私有静态诠释findRow(HSSFSheet片,串cellContent){
        / *
         *这是寻找行号的方法
         * /        INT的rowNum = 0;        对于(鳞次栉比:表){
            (电池单元:行){                而(cell.getCellType()== Cell.CELL_TYPE_STRING){                    如果(cell.getRichStringCellValue()的getString()== cellContent); {                            的rowNum = row.getRowNum();
                            返回的rowNum;
                    }
                }
            }
        }
        返回的rowNum;
    }    私有静态无效完成(){        System.exit(0);
    }
}


解决方案

此方法修复是解决你的问题:

 私有静态诠释findRow(HSSFSheet片,串cellContent){
    对于(鳞次栉比:表){
        (电池单元:行){
            如果(cell.getCellType()== Cell.CELL_TYPE_STRING){
                如果(cell.getRichStringCellValue()的getString()。修剪()。等于(cellContent)){
                    返回row.getRowNum();
                }
            }
        }
    }
    返回0;
}

请记住,你的 colnr 仍然是一个固定值。

I'm looking for a cell in a spreadsheet that has the string 'Total' and then use the row in which that cell is to find the total value in another cell which is always the same cell/column (the 10th cell in a 0 based index).

I have the following code, which has no errors (syntax), but the findCell method is not returning rowNum value:

    public static void main(String[] args) throws IOException{

        String fileName = "C:\\file-path\\report.xls";
        String cellContent = "Total";
        int rownr=0, colnr = 10;

        InputStream input = new FileInputStream(fileName);

        HSSFWorkbook wb = new HSSFWorkbook(input);
        HSSFSheet sheet = wb.getSheetAt(0);

        rownr = findRow(sheet, cellContent);

        output(sheet, rownr, colnr);

        finish();
    }

    private static void output(HSSFSheet sheet, int rownr, int colnr) {
        /*
         * This method displays the total value of the month
         */

        HSSFRow row = sheet.getRow(rownr);
        HSSFCell cell = row.getCell(colnr);

                System.out.println("Your total is: " + cell);
    }

    private static int findRow(HSSFSheet sheet, String cellContent){
        /*
         *  This is the method to find the row number
         */

        int rowNum = 0;

        for(Row row : sheet) {
            for(Cell cell : row) {

                while(cell.getCellType() == Cell.CELL_TYPE_STRING){

                    if(cell.getRichStringCellValue().getString () == cellContent);{

                            rowNum = row.getRowNum();
                            return rowNum;
                    }
                }
            }
        }
        return rowNum;
    }

    private static void finish() {

        System.exit(0);
    }
}
解决方案

This method fix is the solution to your problem:

private static int findRow(HSSFSheet 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)) {
                    return row.getRowNum();
                }
            }
        }
    }
    return 0;
}

Keep in mind that your colnr is still a fixed value.

这篇关于Java的POI:如何找到一个字符串值的Excel单元格并获取其位置(行)使用该位置找到另一个小区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:26