Get columns' names in Excel file using Apache POI

佐手、 提交于 2019-12-17 16:28:51

问题


How to get the column names in an Excel file using Apache POI, to make sure that the columns are ordered as expected.


回答1:


Or this:

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

Row indexes start from 0.




回答2:


There is a convenience method for this:

CellReference.convertNumToColString(cell.getColumnIndex());

To get the full name:

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



回答3:


Apache POI, translating Excel column number to letter

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



回答4:


To get cell name:

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


来源:https://stackoverflow.com/questions/8202319/get-columns-names-in-excel-file-using-apache-poi

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