读取数据作为双数组 POI

Read data as double array POI

我有一个代码可以使用 Apache POI 从 excel sheet 读取数据并将其存储为字符串数组。但我希望这个数据是一个双精度数组,因为我必须出于其他目的对其进行操作。

我尝试使用 Double.parseDouble 将字符串更改为 double。但是从 excel 文件中读取的数据变成了这样的 @8bbab2f.

我也尝试将输出初始化为双精度数组,但出现错误。

那么如何使用 Apache POI 获取双数组数据?

我的代码如下:

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");
    }
    }
    }
}

这应该有效:

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

我只用

测试过
cell instanceof XSSFCell

虽然。根据区域设置,您可能还需要执行以下操作:

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

虽然很可能有更好的解决方案。

您应该使用 getNumericCellValue() 直接返回的双精度值,而不是以一种非常奇怪的方式将其转换为字符串并尝试将其转换回来,即

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");
    }
}

然后将结果值放入double[].

顺便说一句,我还会检查为什么仍然有您要读取的字符串格式的值。如果您将所有值都存储为 Excel sheet 中的数值,那么只需要 CELL_TYPE_NUMERIC-case 就足够了。