使用Apache POI在Excel文件中获取列的名称

如何使用Apache POI在Excel文件中获取列名,以确保按预期排序列。

或这个:

cell.getSheet().getRow(0).getCell(currentcellIndex) .getRichStringCellValue().toString() 

不确定行是否从0开始。希望逻辑有效。

有一种方便的方法:

 CellReference.convertNumToColString(cell.getColumnIndex()); 

要获得全名:

 private static String getCellName(Cell cell) { return CellReference.convertNumToColString(cell.getColumnIndex()) + (cell.getRowIndex() + 1); } 

Apache POI,将Excel列号转换为字母

  FileInputStream fis = new FileInputStream( new File("D:\\workspace\\Writesheet.xlsx")); @SuppressWarnings("resource") XSSFWorkbook workbook = new XSSFWorkbook(fis); XSSFSheet spreadsheet = workbook.getSheetAt(0); int lastcell=spreadsheet.getRow(0).getLastCellNum(); //Non empty Last cell Number or index return for(int i=0;i<=lastcell;i++) { try { System.out.println(CellReference.convertNumToColString(i)); }catch(Exception e) {} } fis.close(); 

获取单元格名称:

 String cellName = new CellAddress(intRow, intCol).toString();