Java Apache poi 如何获取单元格坐标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21896764/
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
Apache poi how to get cell coordinates
提问by Vladimir
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) ?
回答by Gagravarr
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
或者,如果您确实想使用简单的迭代器,则需要从单元格中查找行和单元格索引,例如
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());
}
}
回答by Rahul Karankal
I have done following code for getting coordinates of the excel cell. Hope this will help you.
我已经完成了以下代码来获取excel单元格的坐标。希望这会帮助你。
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");
}
}
回答by Gian Marco Artioli
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()