本文介绍了如果特定列是否具有带有POI的特定文本,如何获取整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在excel电子表格中的特定列的单元格文本中的任意位置过滤单词"GHH".我设法做到了这一点,然后需要返回找到该文本的整行.这无法执行,因为似乎没有一种使用getRowIndex方法来显示整行的方法

I need to filter my excel spreadsheet for the word "GHH" anywhere in the text of a cell in a specific column. I have managed to do this by I then need to have returned the whole row that this text is found in. This I can't do as there doesnt seem to be a way of using the getRowIndex method to then display the whole row.

这是我的代码:

public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream(new File("myfile.xls"));
    HSSFWorkbook workBook = new HSSFWorkbook(fis);
    HSSFSheet sheet = workBook.getSheetAt(0);
    Iterator < Row > rows = sheet.rowIterator();
    while (rows.hasNext()) {
        HSSFRow row = (HSSFRow) rows.next();
        Iterator < Cell > cells = row.cellIterator();
        while (cells.hasNext()) {
            HSSFCell cell = (HSSFCell) cells.next();
            if (cell.toString().contains("GHH")) {
                String key = cell.getStringCellValue();
                int RI = cell.getRowIndex();
            }
        }
    }
    workBook.close();
}

推荐答案

您可以尝试使用List<HSSFRow>将过滤后的行另存为以下内容:

You could try to use a List<HSSFRow> to save filtered rows as bellow:

List<HSSFRow> filteredRows = new ArrayList<HSSFRow>();
Iterator<Row> rows= sheet.rowIterator();
while (rows.hasNext ()){
HSSFRow row = (HSSFRow) rows.next ();
 Iterator<Cell> cells = row.cellIterator ();
 while (cells.hasNext ()){
    HSSFCell cell = (HSSFCell) cells.next ();
    if (cell.toString().contains("GHH")) {
        String key = cell.getStringCellValue();
        int RI=cell.getRowIndex();
        filteredRows.add(row);
        break;
    }
}
// then use filteredRows

这篇关于如果特定列是否具有带有POI的特定文本,如何获取整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:21