Write Double value in numeric cell with specific format in Apache Poi 3.7

前端 未结 3 1793
广开言路
广开言路 2021-01-02 03:14

I need to write a Double value in a numeric cell using a specific format, i mean, the generated xls must have numeric cells containing Double values like, for example: 8,1.

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-02 04:05

    I may be missing something, but I think you just want to style the cell with a format rule to display a single decimal place

    // Do this only once per file
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setDataFormat(
        wb.getCreationHelper().createDataFormat().getFormat("#.#"));
    
    // Create the cell
    Cell c = row.createCell(2);
    c.setCellValue(8.1);
    c.setCellStyle(cellStyle);
    

    That will create a formatting rule, create a cell, set the cell to be the value 8.1, then style it

提交回复
热议问题