2
private String[][] data;

// to read from excel file

public void readFile() throws IOException {

    // Path to the excel file
    // File datafile = new File("C:\\Users\\atomar\\Documents\test.xlsx");
    // A Buffered File Input Stream to read the data
    FileInputStream f = new FileInputStream(
            "C:\\Users\\atomar\\Documents\\test.xlsx");
    System.out.println("file found");
    // InputStream fis = new BufferedInputStream(new FileInputStream("f"));
    // We create a workbook which represents the excel file
    XSSFWorkbook book = new XSSFWorkbook(f);

    // Next a sheet which represents the sheet within that excel file
    XSSFSheet sheet = book.getSheet("Sheet1");

    // No of rows in the sheet
    int rowNum = sheet.getLastRowNum() + 1;
    System.out.println("rowNum");

    // No of columns in the sheet
    int colNum = sheet.getRow(0).getLastCellNum();

    // A Two dimensional array of Strings which represents the data in the
    // sheet
    data = new String[rowNum][colNum];
    {

        for (int i = 0; i < rowNum; i++) {
            // Get the row
            XSSFRow row = sheet.getRow(i);

        for (int j = 0; j < colNum; j++) {
            // Get the columns or cells for the first row and keep
                // looping
                // for the other rows
        XSSFCell cell = row.getCell(j);

        // Make a call to the method cellToString which actually
                // converts the cell contents to String
                data[i][j] = cellToString(cell);

            //  data[i][j] = value;

    // Here is where you write the logic to handle the data.I am
                // just printing out the contents here.

                //System.out.println("The value is " + data[i][j]);


            }
        }

Here I am calling that function

public void InterestedParty() throws InterruptedException {
    try {
        readFile();
    } catch (IOException e1) {
    }
}

Getting error: There are no support for this type of cell I deleted two row data from input excel file then it started before it was working fine. But now I have deleted those row completely the also issue is there

7
  • Where are you getting NPE?.. On which line?? Commented Jan 16, 2014 at 8:09
  • your cell object can be null. you can get the npe at cellToString i think. do you get it there or explicitly at row.getCell(j) Commented Jan 16, 2014 at 8:12
  • a real stacktrace would be helpfull. Commented Jan 16, 2014 at 8:13
  • @paul.cioroianu - His sheet,book,row,cell etc could be null.. Commented Jan 16, 2014 at 8:15
  • @TheLostMind - workbook and sheet are not null. Only the cell or the row can be null, judging by the fact he gets the error here XSSFCell cell = row.getCell(j); and not above Commented Jan 16, 2014 at 8:20

2 Answers 2

7

if there is no data, XSSFCell will be null. Check whether it is null or not. After call your cellToString() method.

    XSSFCell cell = row.getCell(j);
    if(cell != null) {
        data[i][j] = cellToString(cell);
    } else {
        // if you would like to set value when cell is null
        row.createCell(j).setCellValue(your value);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Also, he must check if the row is null or not before callling getCell(j). he can get an empty row too
0

You can have a row with data, an empty row next then another row with data. The middle row can be viewed as null if is undefined, so my advice is to use an iterator for the rows.
RowIteratior here is a good start.
Also, once you have the row, you can use cell iterator to iterate over the cells. Keep in mind that an undefined cell will be viewed as null and skipped, but an empty cell is not null!
Cell iterator is doc'ed here.
Your approach is also good, but you need to check for nulls or empty at each iteration before working with the objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.