Find Excel Cell by Text in Apache POI

2019-02-17 23:40发布

问题:

I'd like to find a cell in an Excel sheet by its text. The text is something like %t:

sheet.findCell("%t"); // pseudo-code, not working

My goal is to enable the user to provide kind of template, in which data is written. Colours and fonts, as well as data's position can be configured by the user in an Excel file. This %t cell is the top-left corner of the data table.

Additional question: Is there a more elegant way to get this job done?

EDIT I'm iterating over the rows and cells to find it. I'm afraid it's not really efficient, but it works so far:

public static Cell findCell(XSSFSheet sheet, String text) {     
    for(Row row : sheet) {          
        for(Cell cell : row) {              
            if(text.equals(cell.getStringCellValue()))
                return cell;
        }
    }       
    return null;
}

回答1:

You can iterate through the cells of the sheet and investigate the contents. I don't think there is an easier method.