Setting Excel cell value in Numeric format using POI

后端 未结 4 1935
忘掉有多难
忘掉有多难 2021-02-19 04:33

I want to store numeric data in excel sheet.
The numeric data is represented by String type in my java code.
Is it possible to set it as numeric without casting it?

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-19 05:15

    You have to provide the value as a double. As the doc says:

    setCellValue

    public void setCellValue(double value)

    set a numeric value for the cell

    Parameters:

    value - the numeric value to set this cell to. For formulas we'll set the precalculated value, for numerics we'll set its value. For other types we will change the cell to a numeric cell and set its value.

    So, it should be

    dCell.setCellValue(new Double("123"));
    

    OR

    dCell.setCellValue(123);  //Remember, the way you did was, you actually passed a string (no quotes)
    

提交回复
热议问题