我试图让一个单元格的坐标,现在我写下面的代码:的Apache POI如何获取单元格的坐标
for (Row row : sheet) {
for(Cell cell : row){
// how to get a cell coordinates?
}
}
我怎样才能得到一个单元格的坐标(DX1,DX2,DY1,DY2)?
我试图让一个单元格的坐标,现在我写下面的代码:的Apache POI如何获取单元格的坐标
for (Row row : sheet) {
for(Cell cell : row){
// how to get a cell coordinates?
}
}
我怎样才能得到一个单元格的坐标(DX1,DX2,DY1,DY2)?
显而易见的解决方案是suggested by user2310289,但不会像迭代器skip over blank rows and cells那样工作。
因此,一种选择是手动循环,以空白单元格的护理,然后你总是知道你在哪里小区所在的代码是这样的:
// 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());
}
}
}
或者,如果你想使用简单的迭代器,你需要从细胞中查找行和单元的指数,如
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());
}
}
我已经做了以下用于获取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");
}
}
我不明白你为什么加入这一行 CellData = row.getCell(j + 1).toString();当你有这个, String CellData = cell.toString(); 无论如何,我赞赏你回答一种如何搜索解决方案。这对我有帮助 –
得到单元格地址的最短途径是:
cell.getAddress()formatAsString()
拿到表名执行以下操作
cell.getSheet()。 getSheetName()
我认为会有更好的方法。然而,看起来OP根据他的其他问题确实想要别的东西。我会删除我的答案。 –
你为什么要在排行榜上排名第一,即1400?这是必要的吗?如果有超过1400行,整个文件将不会被处理。 – RTF
根据您的用例需要设置或删除它,完全取决于您!这只是举一个例子,你如何限制行 – Gagravarr