java 在 Apache POI 中按文本查找 Excel 单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3618763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find Excel Cell by Text in Apache POI
提问by Matthias Meid
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 %tcell is the top-left corner of the data table.
我的目标是使用户能够提供一种模板,其中写入数据。用户可以在 Excel 文件中配置颜色和字体以及数据的位置。该%t单元格是数据表的左上角。
Additional question: Is there a more elegant way to get this job done?
附加问题:有没有更优雅的方法来完成这项工作?
EDITI'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;
}
回答by Thorbj?rn Ravn Andersen
You can iterate through the cells of the sheet and investigate the contents. I don't think there is an easier method.
您可以遍历工作表的单元格并调查内容。我认为没有更简单的方法。

