I want the number of columns of a particular row in excel. How is that possible? I used POI API
我想要excel中某一行的列数。这怎么可能?我使用了POI API
but I could get only columns count to 7 .
但我只能让列数为7。
try
{
fileInputStream = new FileInputStream(file);
workbook = new HSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheet("0");
int numberOfCells = 0;
Iterator rowIterator = sheet.rowIterator();
/**
* Escape the header row *
*/
if (rowIterator.hasNext())
{
Row headerRow = (Row) rowIterator.next();
//get the number of cells in the header row
numberOfCells = headerRow.getPhysicalNumberOfCells();
}
System.out.println("number of cells "+numberOfCells);
}
I want the number of columns at a particular row number say 10 . The excel columns are not same
我想要特定行号上的列数,比如10。excel列不一样。
2 个解决方案
#1
48
There are two Things you can do
你可以做两件事
use
使用
int noOfColumns = sh.getRow(0).getPhysicalNumberOfCells();
or
或
int noOfColumns = sh.getRow(0).getLastCellNum();
There is a fine difference between them
他们之间有细微的差别
- Option 1 gives the no of columns which are actually filled with contents(If the 2nd column of 10 columns is not filled you will get 9)
- 选项1给出了实际填充了内容的列的no(如果10列的第二列没有填充,将得到9)
- Option 2 just gives you the index of last column. Hence done 'getLastCellNum()'
- 选项2给出了上一列的索引。因此做getLastCellNum()”
#2
0
/** Count max number of nonempty cells in sheet rows */
private int getColumnsCount(XSSFSheet xssfSheet) {
int result = 0;
Iterator<Row> rowIterator = xssfSheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
List<Cell> cells = new ArrayList<>();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cells.add(cellIterator.next());
}
for (int i = cells.size(); i >= 0; i--) {
Cell cell = cells.get(i-1);
if (cell.toString().trim().isEmpty()) {
cells.remove(i-1);
} else {
result = cells.size() > result ? cells.size() : result;
break;
}
}
}
return result;
}
#1
48
There are two Things you can do
你可以做两件事
use
使用
int noOfColumns = sh.getRow(0).getPhysicalNumberOfCells();
or
或
int noOfColumns = sh.getRow(0).getLastCellNum();
There is a fine difference between them
他们之间有细微的差别
- Option 1 gives the no of columns which are actually filled with contents(If the 2nd column of 10 columns is not filled you will get 9)
- 选项1给出了实际填充了内容的列的no(如果10列的第二列没有填充,将得到9)
- Option 2 just gives you the index of last column. Hence done 'getLastCellNum()'
- 选项2给出了上一列的索引。因此做getLastCellNum()”
#2
0
/** Count max number of nonempty cells in sheet rows */
private int getColumnsCount(XSSFSheet xssfSheet) {
int result = 0;
Iterator<Row> rowIterator = xssfSheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
List<Cell> cells = new ArrayList<>();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cells.add(cellIterator.next());
}
for (int i = cells.size(); i >= 0; i--) {
Cell cell = cells.get(i-1);
if (cell.toString().trim().isEmpty()) {
cells.remove(i-1);
} else {
result = cells.size() > result ? cells.size() : result;
break;
}
}
}
return result;
}