通过Apache POI中的文本查找Excel单元格

时间:2022-11-05 20:22:56

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

我想在Excel表格中通过文本查找单元格。文本类似于%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.

我的目标是使用户能够提供某种模板,在其中写入数据。用户可以在Excel文件中配置颜色、字体以及数据的位置。这个%t单元格是数据表的左上角。

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 个解决方案

#1


4  

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

您可以遍历表中的单元格,并研究内容。我不认为有更简单的方法。

#1


4  

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

您可以遍历表中的单元格,并研究内容。我不认为有更简单的方法。