I want the number of columns of a particular row in excel. How is that possible? I used POI API
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); }
I want the number of columns at a particular row number say 10 . The excel columns are not same
getLastRowNum() return index of last row. So if you wants to know total number of row = getLastRowNum() +1.
Just click the column header. The status bar, in the lower-right corner of your Excel window, will tell you the row count. Do the same thing to count columns, but this time click the row selector at the left end of the row. If you select an entire row or column, Excel counts just the cells that contain data.
XSSFWorkbook − This class has methods to read and write Microsoft Excel and OpenOffice xml files in . xls or . xlsx format. It is compatible with MS-Office versions 2007 or later.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With