Setting Excel cell value in Numeric format using POI

后端 未结 4 1948
忘掉有多难
忘掉有多难 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)
    
    0 讨论(0)
  • 2021-02-19 05:17

    Old one But Still it This will help Some one from .NET who use NPOI

    dCell.setCellValue(Convert.ToDouble(112.45));
    

    We do have

    cell.SetCellType(NPOI.SS.UserModel.CellType.NUMERIC); 
    

    which doesn't remove the warning but the 1st code did worked after converting to Double for All integers and Double values in .net

    0 讨论(0)
  • 2021-02-19 05:25

    I used Big Decimal for this,

    dCell.setCellValue(new BigDecimal("112.45").doubleValue());
    
    0 讨论(0)
  • 2021-02-19 05:27

    Try to use wrapper classes:

    dCell.setCellValue(new Double(112.45));
    

    BTW, it will work if you simply give the value instead of the double cotes as follows:

    dCell.setCellValue(112.45);
    
    0 讨论(0)
提交回复
热议问题