简体   繁体   中英

how to get integer value from Ms Excel to java

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM