Get number of columns of a particular row in given excel using Java

0 votes

In Excel, I need to know how many columns make up a specific row. How is that even doable? I applied POI API.

but I could only increase the number of columns 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 to know how many columns there are at, let's say, row 10. The columns in Excel are different.

Oct 24 in Others by Kithuzzz
• 20,660 points
105 views

1 answer to this question.

0 votes

Use:

int noOfColumns = sh.getRow(0).getPhysicalNumberOfCells();

Or

int noOfColumns = sh.getRow(0).getLastCellNum();

There is a fine difference between them

  1. Option 1 gives the no of columns that are actually filled with contents(If the 2nd column of 10 columns is not filled you will get 9)
  2. Option 2 just gives you the index of the last column. Hence done 'getLastCellNum()'
answered Oct 24 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

How to get rid of a #value error in Excel?

Changing the format to "Number" doesn't actually ...READ MORE

answered Oct 3 in Others by narikkadan
• 37,660 points
73 views
0 votes
1 answer

Excel - Make a graph that shows number of occurrences of each value in a column

There is probably a better way to ...READ MORE

answered Oct 21 in Others by narikkadan
• 37,660 points
64 views
0 votes
1 answer

Find occurrence count of a number in Excel

First split those integer values into their ...READ MORE

answered Oct 30 in Others by narikkadan
• 37,660 points
43 views
0 votes
1 answer

What is the better API to Reading Excel sheets in java - JXL or Apache POI

Here are the things where both APIs ...READ MORE

answered Oct 9 in Others by narikkadan
• 37,660 points
149 views
0 votes
1 answer

How to get columns from Excel files using Apache POI?

The only way to see all the ...READ MORE

answered Oct 18 in Others by narikkadan
• 37,660 points
253 views
0 votes
1 answer

How to insert a new row in the specified cell only, of Excel sheet using c#?

I have this worksheet with a matrix ...READ MORE

answered 3 days ago in Others by narikkadan
• 37,660 points
8 views
0 votes
1 answer

Is there a maximum number of formula fields allowed in Excel (2010)

See http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx for limits on specs it doesn't indicate ...READ MORE

answered Sep 30 in Others by narikkadan
• 37,660 points
53 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP