简体   繁体   English

Apache poi如何获取单元格坐标

[英]Apache poi how to get cell coordinates

I try to get a cell's coordinates, right now I write next code: 我试着得到一个单元格的坐标,现在我写下一个代码:

for (Row row : sheet) {
   for(Cell cell : row){
      // how to get a cell coordinates?
   }
}

How can I get a cell's coordinates (dx1, dx2, dy1, dy2) ? 如何获得单元格的坐标(dx1,dx2,dy1,dy2)?

The obvious solution was suggested by user2310289 , but that won't work as the iterators skip over blank rows and cells . user2310289提出了明显的解决方案,但由于迭代器会跳过空行和单元格 ,因此无效。

So, one option is to iterate manually, taking care of blank cells, and then you'll always know where you cell is, with code something like: 因此,一种选择是手动迭代,处理空白单元格,然后您将始终知道单元格的位置,代码类似于:

// Decide which rows to process
int rowStart = Math.min(15, sheet.getFirstRowNum());
int rowEnd = Math.max(1400, sheet.getLastRowNum());

for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
   Row r = sheet.getRow(rowNum);

   int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);

   for (int cn = 0; cn < lastColumn; cn++) {
      Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
      if (c == null) {
         // The spreadsheet is empty in this cell
      } else {
         // Do something useful with the cell's contents
         CellReference cr = new CellReference(c);
         System.out.println("Cell at " + rowNum + "," + cn + " found, that's " + cr.formatAsString());
      }
   }
}

Or, if you do want to use the easy iterators, you need to look up the row and cell index from a cell, eg 或者,如果您确实想使用easy迭代器,则需要从单元格中查找行和单元格索引,例如

for (Row r : sheet) {
   for (Cell c : r) {
       int columnNumber = c.getColumnIndex();
       int rowNumber = c.getRow().getRowNum();
       CellReference cr = new CellReference(c);
       System.out.println("Cell at " + rowNum + "," + columnNumber + " found, that's " + cr.formatAsString());
   }
}

the shortest way to get cell address is: 获取单元格地址的最短方法是:

cell.getAddress().formatAsString() cell.getAddress()。formatAsString()

to get sheet name do the following 获取工作表名称执行以下操作

cell.getSheet().getSheetName() cell.getSheet()。getSheetName()

I have done following code for getting coordinates of the excel cell. 我已经完成了以下代码来获取excel单元格的坐标。 Hope this will help you. 希望这会帮助你。

public static void searchSheet(String searchText, XSSFSheet sheet)
{

    int flag=0;
    for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++)
    {
        XSSFRow row = sheet.getRow(i);
        for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++)
        {
            XSSFCell cell = row.getCell(j);
            String CellData=cell.toString();
            if(searchText.equals(CellData))
            {
                flag=1;
                CellData=row.getCell(j+1).toString();
                System.out.println(CellData);
                System.out.println("String Found At Row="+ i +" and At Column="+j);


            }

         }
        break;
    }

    if(flag==0)
    {
        System.out.println("String Not Found");
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM