使用Java获取给定excel中特定行的列数 [英] get number of columns of a particular row in given excel using Java
本文介绍了使用Java获取给定excel中特定行的列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要excel中特定行的列数.这怎么可能?我使用了 POI API
I want the number of columns of a particular row in excel. How is that possible? I used POI API
但我只能得到 7 列.
but I could get only columns count to 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);
}
我想要特定行号的列数,比如 10 .excel列不一样
I want the number of columns at a particular row number say 10 . The excel columns are not same
推荐答案
你可以做两件事
使用
int noOfColumns = sh.getRow(0).getPhysicalNumberOfCells();
或
int noOfColumns = sh.getRow(0).getLastCellNum();
它们之间有细微的差别
- 选项 1 给出实际填充内容的列数(如果 10 列的第 2 列未填充,您将得到 9)
- 选项 2 只为您提供最后一列的索引.因此完成了'getLastCellNum()'
这篇关于使用Java获取给定excel中特定行的列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文