How to get the column names in an Excel file using Apache POI, to make sure that the columns are ordered as expected.
-
5Do you mean "A", "B", ...? If not, what do you mean by column names? Columns are normally "named" by putting names in a top row.Ed Staub– Ed Staub2011-11-20 15:05:11 +00:00Commented Nov 20, 2011 at 15:05
-
3@Ed Staub is it possible to get "A" and "B", i have a similar requirementkavinder– kavinder2014-11-05 12:46:46 +00:00Commented Nov 5, 2014 at 12:46
Add a comment
|
4 Answers
Or this:
cell.getSheet().getRow(0).getCell(currentcellIndex)
.getRichStringCellValue().toString()
Row indexes start from 0.
3 Comments
halfer
If anyone can confirm whether rows start from 0 or not, that would be good. "Not sure if" is not ideal to have in an accepted answer.
user3153014
@halfer I think that depends on how excel sheet in created.
Toan Dao
@halfer Yes. Row's index start from 0.
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();
Comments
To get cell name:
String cellName = new CellAddress(intRow, intCol).toString();
1 Comment
Ahmad Al-Kurdi
please add more explanation