Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to check empty Excel cell using while loop?

Zulfi KhanJul 31 2017 — edited Aug 3 2017

Hi,

I have written a java program to read excel sheets. Its reading a cell which I want. I am using a while loop. My while loop should stop when it encounters a null value. However its not doing this. Its crashing when number of rows are beyond 100. I think my sheet has 100 rows.

Some body please guide me why my loop doesn’t stop when it encounters a null value. My code is:

import java.io.*;

import jxl.Sheet;

import jxl.Workbook;

import jxl.read.biff.BiffException;

import javax.swing.*;

/**

*

* @author HP

*/

public class ReadExcelSheetMain2 {

    /**

     * @param args the command line arguments

     */

    public void readExcel() throws BiffException, IOException {

                                String FilePath = "D:\\Marks-2A-SE-OOP-2015.xls";

                                FileInputStream fs = new FileInputStream(FilePath);

                                Workbook wb = Workbook.getWorkbook(fs);

                                // TO get the access to the sheet

                                Sheet sh = wb.getSheet("Sheet1");

               

                                // To get the number of rows present in sheet

                                int totalNoOfRows = sh.getRows();

JOptionPane.showMessageDialog(null, "totalNoOfRows = " + totalNoOfRows);

                                // To get the number of columns present in sheet

                                int totalNoOfCols = sh.getColumns();

JOptionPane.showMessageDialog(null, "totalNoOfCols = " + totalNoOfCols);

                int row=8;

                int col=1;

                String rollNo = sh.getCell(col, row).getContents();

                while( rollNo != null) {

System.out.print(rollNo+ "\n");

                   rollNo = sh.getCell(col, row).getContents();

                   row++;

                }

System.out.print(sh.getCell(col, row).getContents() + "\t");

                                /*for (int row = 0; row < totalNoOfRows; row++) {

                                                for (int col = 0; col < totalNoOfCols; col++) {

                                                                System.out.print(sh.getCell(col, row).getContents() + "\t");

                                                }

                                                System.out.println();

                                }*/

    }

    public static void main(String[] args)throws BiffException, IOException {

        // TODO code application logic here

        ReadExcelSheetMain2 obj = new ReadExcelSheetMain2();

        obj.readExcel();

    }

   

}

This is my output:

Warning:  Polygon Object on sheet "Sheet1" not supported - omitting

Warning:  Polygon Object on sheet "Sheet1" not supported - omitting

2015-SE-001

2015-SE-001

2015-SE-002

2015-SE-003

2015-SE-004

2015-SE-005

2015-SE-006

2015-SE-007

2015-SE-008

2015-SE-009

2015-SE-010

2015-SE-011

2015-SE-012

2015-SE-013

2015-SE-014

2015-SE-015

2015-SE-016

2015-SE-017

2015-SE-018

2015-SE-019

2015-SE-020

2015-SE-021

2015-SE-022

2015-SE-023

2015-SE-024

2015-SE-025

2015-SE-026

2015-SE-027

2015-SE-028

2015-SE-029

2015-SE-030

2015-SE-031

2015-SE-032

2015-SE-033

2015-SE-034

2015-SE-035

2015-SE-036

2015-SE-037

2015-SE-038

2015-SE-039

2015-SE-040

2015-SE-041

2015-SE-042

2015-SE-043

2015-SE-044

2015-SE-045

2015-SE-046

2015-SE-047

2015-SE-048

2015-SE-049

2015-SE-051

2015-SE-130

2015-SE-138

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100

        at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)

        at ReadExcelSheetMain2.readExcel(ReadExcelSheetMain2.java:41)

        at ReadExcelSheetMain2.main(ReadExcelSheetMain2.java:56)

This post has been answered by unknown-7404 on Aug 1 2017
Jump to Answer

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 31 2017
Added on Jul 31 2017
2 comments
11,758 views