如果特定列具有带有 POI 的特定文本,如何获取整行 [英] How to get the whole row for a if specific column has a certain text with POI

查看:19
本文介绍了如果特定列具有带有 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 将过滤后的行保存如下:

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 的特定文本,如何获取整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆