how to move the active cell to the top left cell i

2019-06-02 06:53发布

问题:

I am trying to migrate the java excel code from JBook to XSSFWorkbook. Am not getting exact methods that are used in JBook. Some of them are:

JBook.setSelection(int row, int col, int row, int col);
JBook.setSelection(JBook.getDefinedName("some String"));

The first method explains moving the active cell to the top left cell in the range.
Is there any way to get the rowindex and column index from XSSFWorkbook object?

回答1:

You can't set active cell via workbook object. Possible options:

  • if you have cell object you can mark it as active: cell.setAsActiveCell();
  • if you work with XSSFSheet and want to set multiple cell selection: xssfSheet.setActiveCell(reference) (works with xmlbeans 2.6.0, not 2.3.0)

Reference can be set as a string: "A1:A2" or using CellReference class:

String reference = new CellReference(rowNum, colNum).formatAsString()

Is there any way to get the rowindex and column index from XSSFWorkbook object?

You can get index from the cell:

cell.getColumnIndex()
cell.getRowIndex()

I don't know what is JBook.getDefinedName("some String") but if it works with named ranges, check POI user guide: Named Ranges and Named Cells.



回答2:

Did you try this way

         HSSFWorkbook book1 = new HSSFWorkbook();
         HSSFSheet sheet = book1.getSheet("sheetName");
         int rowIndex=sheet.getLastRowNum()+1;//
         int columnIndex = sheet.getRow(sheet.getLastRowNum()).getPhysicalNumberOfCells()