是否可以使用java在excel xssf文档中查找文本并获取单元格索引,即[行号和列数]?

时间:2016-12-01 12:45:43

标签: java apache-poi

使用java查找并获取该文本的列和行值。 在Xssf文档中。

使用java

查找在excel文件中搜索的文本的位置

1 个答案:

答案 0 :(得分:3)

Apache POI documentation中介绍了这一点,我们非常建议您阅读这些内容!

具体而言,在iterating over rows and cellsgetting the cell contents

从那里获取代码和您的需求,我们得到:

String toFind = "needle in haystack";

Workbook wb = WorkbookFactory.create(new File("input.xlsx"));
DataFormatter formatter = new DataFormatter();
Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
    for (Cell cell : row) {
        CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());

        // get the text that appears in the cell by getting the cell value and applying any data formats (Date, 0.00, 1.23e9, $1.23, etc)
        String text = formatter.formatCellValue(cell);

        // is it an exact match?
        if (toFind.equals(text)) {
           System.out.println("Text matched at " + cellRef.formatAsString());
        }
        // is it a partial match?
        else if (text.contains(toFind)) {
           System.out.println("Text found as part of " + cellRef.formatAsString());
        }
    }
}