Sandy.Arv
Sandy.Arv

Reputation: 605

Read data as double array POI

I have a code which reads data from an excel sheet using Apache POI and stores it as an array of strings. But I want this data to be an array of double, since i have to manipulate it for other purposes.

I tried changing the string to double using Double.parseDouble. but the data that is read from the excel file becomes something like this @8bbab2f.

I also tried initialising the output as a double array but throws an error.

So how do I get data as double array using Apache POI?

My code is as follows:

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;



public class XLRead {

public static void main(String args[]) throws Exception {
    Scanner in = new Scanner(System.in);
    String wb_name;
    wb_name = in.next();

    File excel = new File("/Users/Documents/Sample_data/" + wb_name);
    FileInputStream fis = new FileInputStream(excel);


    HSSFWorkbook wb = new HSSFWorkbook(fis);
    HSSFSheet sh = wb.getSheet("sample");

    int rowNum = sh.getLastRowNum() + 1;
    //System.out.println(rowNum);
    int colNum = sh.getRow(0).getLastCellNum();
    //System.out.println(colNum);

    String[][] X0 = new String[rowNum][colNum];
    // Here i tried to define X0 as double[][]. But it doesn't work.
    for (int i = 3; i < 368; i++){
        HSSFRow row = sh.getRow(i);
    //System.out.println(row);
        for (int j = 4; j <= 27; j++){
            HSSFCell cell = row.getCell(j);
            //System.out.println(cell);     
            String value = cellToString(cell);
            X0[i][j] = value;               
            //System.out.println(value);
        }
    }
    } 
public static String cellToString(HSSFCell cell){
    // i tried changing this function to public static double cellToDouble(HSSFCell cell)
    int type;
    Object result = null;
    type  = cell.getCellType();
    if(cell!=null){
    if (type == Cell.CELL_TYPE_FORMULA)
    {
    switch (cell.getCachedFormulaResultType()){
        case Cell.CELL_TYPE_NUMERIC:
            result = cell.getNumericCellValue();

            break;
        case Cell.CELL_TYPE_STRING:
            result = cell.getStringCellValue();
            break;
        default:
            throw new RuntimeException("Data type not applicable");
    }
    }
    }
}

Upvotes: 0

Views: 1565

Answers (2)

centic
centic

Reputation: 15872

You should use the double value that getNumericCellValue() is returning directly without converting it to String in a very strange way and trying to converting it back, i.e.

public static double getCellValue(HSSFCell cell){

    switch (cell.getCachedFormulaResultType()){
        case Cell.CELL_TYPE_NUMERIC:
            return cell.getNumericCellValue();

            break;
        case Cell.CELL_TYPE_STRING:
            return Double.parse(cell.getStringCellValue());
           break;
        default:
            throw new RuntimeException("Data type not applicable");
    }
}

and then put the resulting value into a double[].

BTW, I also would check why there are still String-formatted values that you are trying to read. If you store all values as numeric values in the Excel sheet, only having the CELL_TYPE_NUMERIC-case should suffice.

Upvotes: 2

makrom
makrom

Reputation: 376

This should work:

final DataFormatter dataFormatter = new DataFormatter();
String cellValueAsString dataFormatter.formatCellValue(cell);
double cellValueAsDouble = Double.parseDouble(cellValueAsString);

I have only tested it with

cell instanceof XSSFCell

though. Depending on regional settings, you might also need to do something like this instead:

double cellValueAsDouble = Double.parseDouble(cellValueAsString.replace(',', '.'));

There are most likely better solutions though.

Upvotes: 2

Related Questions