[英]Get columns' names in Excel file using Apache POI
如何使用Apache POI在Excel文件中获取列名,以确保按预期顺序排列列。
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);
}
Or this: 或这个:
cell.getSheet().getRow(0).getCell(currentcellIndex)
.getRichStringCellValue().toString()
Row indexes start from 0. 行索引从0开始。
Apache POI, translating Excel column number to letter 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.