在 Apache POI 中按文本查找 Excel 单元格 [英] Find Excel Cell by Text in Apache POI

查看:52
本文介绍了在 Apache POI 中按文本查找 Excel 单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过文本在 Excel 工作表中查找单元格.文本类似于 %t:

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

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

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 我正在遍历行和单元格以找到它.恐怕它不是很有效,但到目前为止它有效:

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;
}

推荐答案

您可以遍历工作表的单元格并调查内容.我认为没有比这更简单的方法了.

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

这篇关于在 Apache POI 中按文本查找 Excel 单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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