0

I imported Apache.POI package and want to get integer value from Excel cell to Java console. I know how get the string value from excel cell to Java console

System.out.println(sh1.getRow(0).getCell(2).getRichStringCellValue().toString());

but i don't know how to get integer value. Please help me.

The code is below.

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.util.WorkbookUtil; 

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;

public class Excel {

    public static void main(String[] arg){
        Workbook workbook = new HSSFWorkbook();

        Sheet sh1 = workbook.createSheet("sheet1");

        Cell cell1 = sh1.createRow(0).createCell(0);
        Cell cell2 = sh1.createRow(0).createCell(1);
        Cell cell3 = sh1.createRow(0).createCell(2);


        cell1.setCellValue(100);
        cell2.setCellValue(200);
        cell3.setCellFormula("A1+B1");


        //Here!! I want to get integer value of cell3
        System.out.println(sh1.getRow(0).getCell(2).....);

        ..........
    }

}

thanks

2
  • Did you take a look at Busy Developers' Guide? Commented Nov 27, 2014 at 7:20
  • i have to look that up! Commented Nov 27, 2014 at 7:24

1 Answer 1

1

Use the function getNumericCellValue(). See the documentation of HSSFCell for more information.

Sign up to request clarification or add additional context in comments.

5 Comments

i tried .. but there is error msg with "Exception in thread "main" java.lang.NullPointerException at Excel.Excel.main(Excel.java:36)"
@imhyung Try to find out wich value is null. sh1, sh1.getRow(0) ... it has nothing to do with the numeric value.
and 'getNumbericCellValue()' returns double, so i also made return value to double. but still shows the same error msg.
Looks like sh1.getRow(0) or sh1.getRow(0).getCell(2) is null. So you get the NPE
cell3.getNumbericCellValue()is working instead sh1.rownumber.cellnumber.getNumbericCellValue() Thanks man!!

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.