Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  1. 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)
  2. Option 2 just gives you the index of last column. Hence done 'getLastCellNum()'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...