how to move the active cell to the top left cell in the range apache poi using XSSFWorkbook?

时间秒杀一切 提交于 2019-12-23 03:51:59

问题


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()


来源:https://stackoverflow.com/questions/34305956/how-to-move-the-active-cell-to-the-top-left-cell-in-the-range-apache-poi-using-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!